const issues and more!

Neil Vice psgdg at swiftdsl.com.au
Mon Jan 14 05:04:41 PST 2008


Thanks for the reply.

> How about:
>
> char[] text = "text".dup;
>
> .dup creates a mutable copy. There's also .idup, for an immutable copy, 
> but I don't know its semantics.

You are of course correct, however it seems a tad unnecessarily verbose 
IMHO.

> I guess this is the same thing as doing the following:
>
> const int y;
> foo(y);
>
> void foo(int n) {
> n = n+1;
> }
>
> I.e. since you don't have ref, it's just a copy of the value which is 
> modified. You'll note that the original test array is unmodified even if 
> it's not const:
>
> char[] foo = "foo";
> foreach (c; foo)
> c = 'x';
> assert (foo == "foo"); // passes

I had overlooked that. I don't see the utility of having a modifiable local 
copy however and it strikes me as a litle confusing and error-prone. I would 
expect that the common case is to want a const reference, which ends up 
being the most verbose declaration unfortunately.

> If DMD crashes and this isn't in the Bugzilla, file the bug.

It would appear the bug has indeed been filed (several times =P).

> If 'ref const' does work it's probably one or the other. DMD has a habit 
> of accepting contradictory attributes:

>From browsing other posts I get the impression that it is a way of passing 
structs as readonly refernces, however I would have expected just const to 
function the same.

> public private protected int x; // quiz: what is the protection level of 
> x?

My guess would be protected but the fact that it compiles seems ridiculous.

> Use 1.0 and live happily, waiting for the const stuff to be finalized. ;-)

I may have to do just that but I do love my const =) 





More information about the Digitalmars-d mailing list