Stepping back and looking at constness from another angle.

Bill Baxter dnewsgroup at billbaxter.com
Mon Jun 4 19:47:16 PDT 2007


Jason House wrote:
> Charlie wrote:
>> Or the opposite is probably better, make it always error out when the 
>> function tries to modify it, and only when you expect it to do the 
>> modifying you pass it : foo( mod myInstance );  ( Ignore the syntax, 
>> just the idea ).
> 
> Honestly, when I first saw in, out, and inout, I thought that was sort 
> of the idea... with in being the default.  I considered in to be 
> "const&" or "const" depending on efficiency and inout to be "&". (Excuse 
> the mixing of C++-like terms).  I liked the simplicity of it. Then I 
> discovered that objects are really pointers and the description really 
> only applies to the pointer for them.  That's when I had my "whoa, this 
> is really wrong" moment.  The lack of const was one of my big gripes 
> with the D language.
> 
> I'm glad to see that they're working on it.  I'm sad that I don't see 
> any documentation on what the new design will (or won't) be. Personally, 
> I think "const final scope" seems like a mouthful.  I'll never want to 
> type it.  

That's why you'll only have to type 'in' in Walter's latest vision 
posted here.  'in' on a parameter will be synonymous with 'const scope 
final' so you won't have to think about it.  But you will have to use 
'in' explicitly if that's what you want.  I suspect if you want things 
to const, you'll make objects in and structs in ref.  But as I said 
before it would be really cool if structs could just be 'in' too without 
me having to guess whether Vec2f is big enough that it should be passed 
by reference or not.

> Even typing two keywords seems like a lot to me... kind of why 
> I liked the simplicity of in, out, and inout.  If in meant what I 
> thought it meant, I probably would have few gripes.

That's kind of the feeling I had too originally.  I expected that (like 
the simplicity of no virtual in D) that the compiler was going to figure 
out the best way to pass parameters to match the stated intent 
(in/out/inout).  But I think that's basically what we'll have.



More information about the Digitalmars-d mailing list