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

monarch_dodra monarchdodra at gmail.com
Fri Aug 2 07:09:49 PDT 2013


On Friday, 2 August 2013 at 13:47:06 UTC, Nicolas Sicard wrote:
>
> String mixins are fun, but you could also use a more classic 
> template:
> ---
> void checkNonNull(alias var)() {
>     if (var is null)
>     	throw new Exception(var.stringof ~ " cannot be null.");
> }
>
> void main() {
> 	string s;
> 	checkNonNull!s;
> }
> ---
>
> Or with file and line:
> ---
> void checkNonNull(alias var)(string file = __FILE__, size_t 
> line = __LINE__) {
>     if (var is null)
>     	throw new Exception(var.stringof ~ " cannot be null.", 
> file, line);
> }
> ---

Good point! Alias parameter for the win!

With this, there is simply no reason to use a mixin.


More information about the Digitalmars-d-learn mailing list