How to create mixin template and pass name of a variable?
Andrej Mitrovic
andrej.mitrovich at gmail.com
Fri Aug 2 04:48:31 PDT 2013
On Friday, 2 August 2013 at 11:37:27 UTC, Bosak wrote:
> I want to create a mixin template such that:
>
> mixin template ArgNull(alias arg, string name)
> {
> if(arg is null)
> throw new Exception(name~" cannot be null.");
> }
>
> But is there a way to do that with only one template argument.
> And then use it like:
>
> string text = null;
> mixin ArgNull!(text);
>
> And the above mixin to make:
>
> if(text is null)
> throw new Exception("text cannot be null.");
Note that template mixins cannot inject arbitrary code, they can
only inject declarations. You can use '__traits(identifier,
symbol)' on an alias parameter to retrieve the symbol name, for
example:
-----
mixin template ArgNull(alias arg)
{
void test()
{
if (arg is null)
throw new Exception(__traits(identifier, arg) ~ "
cannot be null.");
}
}
string text = null;
mixin ArgNull!text;
void main()
{
test();
}
-----
More information about the Digitalmars-d-learn
mailing list