How to get value of type at CT given only an alias
Moritz Maxeiner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jul 14 14:02:32 PDT 2017
On Friday, 14 July 2017 at 18:06:49 UTC, Steven Schveighoffer
wrote:
>
> .init is the default value.
>
> I'm not sure you can get the default value of a non-default
> initializer, My attempts using init didn't work. e.g.:
>
> void foo(alias T)()
> {
> pragma(msg, T.init);
> }
>
> struct S
> {
> int y = 5;
> void bar() { foo!y; } // prints 0
> }
See spec [1]:
"If applied to a variable or field, it is the default initializer
for that variable or field's type."
If you want to get at the 5 in a static context, you'd have to use
---
S.init.y
---
i.e. get the default initializer for the struct and get its y
member's value.
[1] https://dlang.org/spec/property.html#init
More information about the Digitalmars-d-learn
mailing list