Constants, Aliases

Lutger lutger.blijdestijn at gmail.com
Thu Dec 14 04:18:05 PST 2006


Tomas Lindquist Olsen wrote:
> Lutger wrote:
> 
>> Xinok wrote:
>>> The problem with the const keyword is it doesn't guarantee the
>>> expression will be constant.
>> It doesn't? How can you modify a const var then? (in D)
> 
> This actually feels more like a bug, but here's an example:
> 
> module noconst;
> import std.stdio;
> struct S {
>     float f;
> }
> const S s = {3.1415};
> void main() {
>     writefln(s.f);
>     s.f = 666;
>     writefln(s.f);
> }
> 
> (DMD.177)

Huh yes, const doesn't work at all for structs. This also compiles:

struct S
{
     const int i;
}

void main()
{
     S s;
     s.i = 10;
     s.i = 666;
}



More information about the Digitalmars-d mailing list