Trait for "can be instantiated"?

Ben Jones fake at fake.fake
Tue May 10 15:26:28 UTC 2022


On Tuesday, 10 May 2022 at 05:45:25 UTC, ag0aep6g wrote:
>
> `x` is a type, period.
>
> You can use void initialization to declare values of types that 
> don't have an `init` value: `x value = void;`
>
> As for an alternative to the brute force `__traits(compiles, 
> ...)`, you can check if `T.init` is a thing:
>
>     static if (is(typeof(T.init))) { T value; }
>
> I'm not sure if that's really better, though.
>
> By the way, what is your `Wrap` supposed to do with `x`? 
> Treating it like `y` will likely fail, too, because `x` is not 
> a value.

I'm writing a lexer and I'm using sumtype to store any of the 
token types.  Some have values associated with them (like 
brackets and parens which are defined as `enum lparen = '('` or 
whatever) and some are just markers (keywords like 'if', which 
I'm trying to represent with just `enum if_token` ). The wrapper 
struct is there because I need a type for each one to use them as 
part of a sumtype and I only want to store the enum's value when 
it makes sense to.


More information about the Digitalmars-d-learn mailing list