How to create mixin template and pass name of a variable?

Tobias Pankrath tobias at pankrath.net
Fri Aug 2 04:52:45 PDT 2013


On Friday, 2 August 2013 at 11:50:05 UTC, Bosak wrote:
> 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.");
>
> I tested my code above and it seems it doesn't work either. I 
> came with an idea to
> write:
>
> mixin template ArgNull(string arg)
> {
>     if(mixin(arg) is null)
>         throw new Exception(arg~" cannot be null.");
> }
>
> But it seems that I can't put an if in a template like that. Is 
> there a way to
> implement this?

void argNull(alias var)() { if(var is null) throw ... } ?

and then just call this template function.


More information about the Digitalmars-d-learn mailing list