struct init property
Ali Çehreli
acehreli at yahoo.com
Thu Aug 23 21:24:49 PDT 2012
On 08/23/2012 12:38 PM, Dmitry Olshansky wrote:
> On 23-Aug-12 23:33, Namespace wrote:
>> But to write your own init property would still be nice, or not?
>
> Either way it has to be CTFEable. Can't you just do:
>
> struct A{
> X field = generateX();
> }
>
> and thus set .init to whatever you want.
> Otherwise it will inevitably rise question of the point of "= x;" syntax
> if initializes are overridden by custom .init.
>
Yes, that works:
struct S
{
int i = init_i();
double d = init_d();
static int init_i()
{
return 42;
}
static double init_d()
{
return 1.5;
}
}
void main()
{
assert(S().i == 42);
assert(S().d == 1.5);
}
So the language doesn't give us the ability to define a CTFE-able
default constructor but writing individual "constructors" for each
member works.
Ali
More information about the Digitalmars-d-learn
mailing list