The Status of Const

Jonathan M Davis jmdavisprog at gmail.com
Thu Aug 12 16:36:10 PDT 2010


On Thursday, August 12, 2010 15:56:50 dsimcha wrote:
> This is from a discussion that originated on the Phobos mailing list, but I
> thought I'd bring up the question of what should be done about const on the
> newsgroup to see what others think:
> 
> Despite its theoretical beauty, I find D's const/immutable system to be
> utterly useless for all but the simplest use cases.  I made a serious
> attempt a while back to use it in a real multithreaded program.  In
> hindsight it was more trouble than it was worth, largely for three
> reasons:
> 
> 1.   It's difficult to create non-trivial immutable data structures, and
> often impossible without relying on either unchecked casts or unnecessary
> copying.
> 
> 2.  Much generic code in Phobos (even things as simple as std.math.pow()
> before I recently fixed it) behaves incorrectly when given const/immutable
> data.  This also applies to other libraries I use, including ones that I'm
> the main author of, so I'm just as guilty of it as anyone.  Given that
> noone, including me, seems to be able to get const to interact well with
> generic code, perhaps we need a language-level solution.
> 
> 3.  inout is currently so bug-ridden it's not even funny.  (Though this is
> clearly fixable long-term, once we get higher priority stuff off our
> plates.)
> 
> It would have probably been better if this was brought to a head sooner,
> but it's better late than never.  Do others agree that D's const system is
> difficult to impossible to use properly?  Has anyone successfully used D's
> const system in a non-trivial setting despite these limitations?  If so,
> was it more trouble than it was worth in hindsight?  How can these
> limitations be worked around and/or fixed?

I've tried to use const, but it typically doesn't happen much because of the 
bugs with inout and the fact that Object isn't yet const-correct (that being a 
huge const-killer). Also, the fact that you have to use Rebindable is highly 
annoying. Still, aside from the bugs, const doesn't seem all that bad to deal 
with (still I just don't have enough experience with it due to aforementioned 
bugs). immutable is far worse because there's no easy way (certainly no generic 
way) to get immutable versions of stuff other than arrays. At least with const, 
you can generally pass non-const stuff to it and it's fine, but you can't do the 
same with immutable. The lack of a mutable qualifier seems like it could be 
another big problem, but I haven't yet written enough code in D to run into that 
issue (still I'm sure I'll run into it eventually and be highly annoyed - it's 
just needed too often in C++ for me not to be happy about it missing in D).

I'm a huge fan of const in C++, and I _really_ want to be able to use it in D 
(the lack of const in D1 is one of the big reasons that chose to use D2 even 
when it was new rather than mess with D1), but the bugs related to it have made 
it difficult, and I just haven't written very much large stuff in D - and that's 
where you really need it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list