Trait for "can be instantiated"?
Basile B.
b2.temp at gmx.com
Wed May 11 12:29:05 UTC 2022
On Tuesday, 10 May 2022 at 16:10:26 UTC, Ben Jones wrote:
> On Tuesday, 10 May 2022 at 16:05:15 UTC, H. S. Teoh wrote:
>> Using wrapper structs, etc., for this is IMO total overkill.
>> Just use an enum for your token types. Something like this
>> would suffice:
>
> That's basically what sumtype is going to do for me, but
> (hopefully) more safely. Also, the token types are "user
> defined," my lexer just grabs everything annotated with @Token
> and passes those types/wrapped enums to sumtype.
How about being more explicit in the UDA ?
The idea would be to associate the enum value to a type or not:
```d
import std.traits;
import std.stdio;
struct Token(T);
struct Token(T...)
if (T.length == 0) { }
@Token!(string) enum str_tok;
@Token!(float) enum float_tok;
@Token!() enum lparen_tok;
void main()
{
alias toks = getSymbolsByUDA!(mixin(__MODULE__), Token);
static foreach (t; toks)
{{
alias U = getUDAs!(t, Token);
alias A = TemplateArgsOf!(U);
static if (A.length)
pragma(msg, "add a `" ~ A[0].stringof ~ "`for `" ~
t.stringof ~ "`");
else
pragma(msg, "no SumType data needed for `" ~
t.stringof ~ "`");
}}
}
```
More information about the Digitalmars-d-learn
mailing list