Can we drop static struct initializers?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Nov 20 16:47:29 PST 2009


Bill Baxter wrote:
> On Fri, Nov 20, 2009 at 3:34 PM, Walter Bright
> <newshound1 at digitalmars.com> wrote:
>> Bill Baxter wrote:
>>> Here's one thing I just found:
>>> struct constructors don't work at compile-time:
>>>
>>> struct Struct
>>> {
>>>    this(int _n, float _x) {
>>>        n = _n; x = _x;
>>>    }
>>>    int n;
>>>    float x;
>>> }
>>>
>>> enum A = Struct(1,2);
>>>
>>> // Error: cannot evaluate ((Struct __ctmp1;
>>> // ) , __ctmp1).this(1,2F) at compile time
>>>
>>> The C-style initializer works.
>>> static opCall works too.
>>>
>>> But if that bug is fixed, then I can't think of a reason to have the
>>> classic C-style no-colons syntax.
>> It isn't a bug. You simply don't need constructors that progressively assign
>> parameters to fields.
>>
>>   Struct(1,2);
>>
>> works just fine without that constructor being defined.
> 
> Right, but if you do define it (in order to do something extra upon
> initialization -- validate inputs or what have you) then it no longer
> works at compile time.
> 
> --bb

This is where CTFE should come to save the day.

Andrei



More information about the Digitalmars-d mailing list