January 01, 2008
Jarrett Billingsley wrote:
> ""J�r�me M. Berger"" <jeberger@free.fr> wrote in message news:flea9n$1n0r$1@digitalmars.com...
> 
>> Untested:
>>
>> const(C) c =  (delegate const(C) () {
>>   if (someCondition)
>>      return new C(1);
>>   else
>>      return new C(2);
>> })();
> 
> I'm crying.
> 
	In fact, there's much simpler:

const (C) c = (someCondition) ? (new C (1)) : (new C (2)),

		Jerome
- --
+------------------------- Jerome M. BERGER ---------------------+
|    mailto:jeberger@free.fr      | ICQ:    238062172            |
|    http://jeberger.free.fr/     | Jabber: jeberger@jabber.fr   |
+---------------------------------+------------------------------+
January 01, 2008
Jarrett Billingsley wrote:
> Solve:
> 
> const(C) c;
> 
> if(someCondition)
>     c = new C(1);
> else
>     c = new C(2); 

C cx;
if (someCondition)
	cx = new C(1);
else
	cx = new C(2);
const(C) c = cx;
January 01, 2008
""Jérôme M. Berger"" <jeberger@free.fr> wrote in message news:fleb1b$1ol5$1@digitalmars.com...

> const (C) c = (someCondition) ? (new C (1)) : (new C (2)),

OK.  I guess the point I was trying to make before is that you can't generally combine declaration and initialization.  I'm sure you can all imagine a more complex example that would be prohibitive/stupid to try to initialize in a single expression.


January 01, 2008
"Walter Bright" <newshound1@digitalmars.com> wrote in message news:flebgf$1pmb$1@digitalmars.com...

> C cx;
> if (someCondition)
> cx = new C(1);
> else
> cx = new C(2);
> const(C) c = cx;

...


January 01, 2008
Jarrett Billingsley wrote:
> ""Jérôme M. Berger"" <jeberger@free.fr> wrote in message news:fleb1b$1ol5$1@digitalmars.com...
> 
>> const (C) c = (someCondition) ? (new C (1)) : (new C (2)),
> 
> OK.  I guess the point I was trying to make before is that you can't generally combine declaration and initialization.  I'm sure you can all imagine a more complex example that would be prohibitive/stupid to try to initialize in a single expression. 
> 
> 
General solution? (untested)
1) define a function to return a value of the desired type and value.
2) use it to assign a value to a constant.

I suspect that this function might need to be static, but I seem to remember that const is intentionally limited to rom-able values, so that shouldn't be too restrictive.

It's still not as pleasant as being able to assign once.
January 02, 2008
> C cx;
> if (someCondition)
>     cx = new C(1);
> else
>     cx = new C(2);
> const(C) c = cx;

Wouldn't it be good if there was a way to express
"I will not modify this anymore", instead of creating a new object?
C c;
if (someCondition)
c = new C(1);
else
c = new C(2);
cast(invariant) c; //from now on, c will not change anymore
//more code ...

The last line could do sth like:
{ //new scope
invariant c = cast(invariant) c; //shadowing declaration
//more code ...
} //ends just before the ...
} //... end of the outer scope


Best regards,
Daniel
January 02, 2008
Jarrett Billingsley wrote:
> "Walter Bright" <newshound1@digitalmars.com> wrote in message news:flebgf$1pmb$1@digitalmars.com...
> 
>> C cx;
>> if (someCondition)
>> cx = new C(1);
>> else
>> cx = new C(2);
>> const(C) c = cx;
> 
> ... 

Note that this isn't any different from what you'd write for a const int:

    int ix;
    if (someCondition)
	ix = 1;
    else
	ix = 2;
    const(int) i = ix;

or const of any type.
January 02, 2008
Walter Bright wrote:

> New const/invariant/enum in 2.009!
> 
> http://www.digitalmars.com/d/1.0/changelog.html http://ftp.digitalmars.com/dmd.1.025.zip
> 
> http://www.digitalmars.com/d/changelog.html http://ftp.digitalmars.com/dmd.2.009.zip

Has the const/invariant/enum documentation been updated yet?
Looking at http://www.digitalmars.com/d/const3.html, there is no mention of
enum.  It does discuss manifest constants...

Can you please update the docs to reflect the new design?
January 02, 2008
Reply to Jarrett,

> "Walter Bright" <newshound1@digitalmars.com> wrote in message
> news:fle8mi$1j24$2@digitalmars.com...
> 
>> ???? (noshiika) wrote:
>> 
>> Replace:
>> 
>>> const(C) c;
>>> c = new C(1); // error
>> with:
>> 
>> const(C) c = new C(1);
>> 
> Solve:
> 
> const(C) c;
> 
> if(someCondition)
> c = new C(1);
> else
> c = new C(2);

const(C) c =    (someCondition) ?
            new C(1) :
            new C(2);

or:

const(C) c = new C((someCondition) ? 1 : 2);


January 02, 2008
Jason House wrote:
> Has the const/invariant/enum documentation been updated yet?
> Looking at http://www.digitalmars.com/d/const3.html, there is no mention of
> enum.  It does discuss manifest constants...
> 
> Can you please update the docs to reflect the new design?

See http://www.digitalmars.com/d/enum.html for the enum stuff.