Other language features you'd want in D

Simen Kjaeraas simen.kjaras at gmail.com
Sat Feb 23 16:28:06 PST 2008


On Sun, 24 Feb 2008 00:19:59 +0100, Robert Fraser  
<fraserofthenight at gmail.com> wrote:

> Alexander Panek wrote:
>> Robert Fraser wrote:
>>> Constant initialization can already be paired with declaration for  
>>> non-class types:
>>>
>>> const(int) HTTP_PORT = 80;
>>>
>>> But it can't be for class types at global scope:
>>>
>>> const(Port) HTTP_PORT = new Port(80);
>>>
>>>
>>> How is making it valid for (primitives, arrays, structs, anything with  
>>> a constant initializer) but not for classes "keeping initialization in  
>>> one place"?
>>  You're having a compile-time literal vs. a runtime object here. That's  
>> kinda a big difference, you know? I wouldn't want to have my runtime  
>> object initialization to be somewhere in class declaration scope. Kinda  
>> hard to measure the performance then.
>>  Oh, besides... this wouldn't even be in static constructor, but rather  
>> the normal constructor.
>>  class A {
>>     Port httpPort;
>>      this (int port = 80) {
>>         httpPort = new Port(port);
>>     }
>> }
>>  I'd like to keep the "verbosity" of initializing class members at  
>> runtime as-is.
>
> You don't need a class for that, as you probably know. What I want is  
> for:
>
> const(Port) HTTP_PORT = new Port(80);
>
> to be equivalent to:
>
> Port HTTP_PORT;
> static this() { HTTP_PORT = new Port(80); }
>
> except, you know, allowing the const in there somewhere.


Something like this was mentioned a few months back. That any compile-time  
initialization the compiler wasn't able to do at compile time, should be  
automagically moved to a static this. I don't remember what Walter said  
about it, though.
IMO it sounds like a nice feature, but one that should show a warning.



More information about the Digitalmars-d mailing list