how to handle void arguments in generic programming ?

Dicebot public at dicebot.lv
Mon Nov 11 05:04:23 PST 2013


On Monday, 11 November 2013 at 03:52:16 UTC, Timothee Cour wrote:
> The code snippet below doesn't work. Is there a way to make it 
> work?
>
> import std.stdio;
> void main(){
>   writelnIfNonVoid(writeln("ok"));
> }
> void writelnIfNonVoid(T...)(T a){
>   static if(T.length)
>     writeln(a);
> }

Considering "cannot have parameters of type void" error, I doubt 
it. Using lambda can do as workaround though (returning void is 
legal):

import std.stdio;
import std.traits;

void main()
{
	writelnIfNonVoid(() { writeln("ok"); });
}

void writelnIfNonVoid(T)(T f)
	if (isSomeFunction!T)
{	
	static if (!is(ReturnType!T == void))
		writeln(a);
}


More information about the Digitalmars-d-learn mailing list