How to pass alias as template parameter?

Andrey saasecondbox at yandex.ru
Mon Aug 27 09:50:01 UTC 2018


Hello,
This code doesn't compile:

-----------------------------------------------------------------------
import std.meta;
import std.stdio;

enum Option : string
{
     First = "-first" ,
     Second = "-second",
     Qwerty = "-qwerty"
}

void handler(Option option)(string[] args, ref ushort index)
{
     writeln("Case: ", args[index]);
}

void handler(string arg, ref ushort index)
{
     writeln("Default: ", arg, " at index ", index);
}

alias Pair(alias key, alias value) = AliasSeq!(key, value);
alias Pairs = AliasSeq!(Pair!(Option.First, 
handler!(Option.First)), Pair!(Option.Second, 
handler!(Option.Second)), handler);

void parseArgs(alias sequence)(ushort index, string[] args)
{
     alias last = sequence[$ - 1];
     alias pairs = sequence[0 .. $ - 2];

     for(ushort i = index; i < args.length; ++i)
     {
         string arg = args[i];
         switch(arg)
	    {
	        static foreach(pair; pairs)
	        {
	            case pair.key:
	                pair.value(args, i);
	                break;
	        }
	
	        default:
                 last(arg, i);
                 break;
	    }
     }
}

void main()
{
     ushort index = 1;
     string[] args = ["-second", "123", "-qaz", "true", "-first", 
"77", "value"];
     parseArgs!Pairs(index, args);
}
-----------------------------------------------------------------------

Output:
>onlineapp.d(52): Error: template instance `parseArgs!("-first", 
>handler, "-second", handler, handler)` does not match template 
>declaration parseArgs(alias sequence)(ushort index, string[] 
>args)

I don't understand how to pass my "Pairs" alias into template 
function "parseArgs".

P.S. Yes, I know that exists "getopt".


More information about the Digitalmars-d-learn mailing list