-
-
Save crazymonkyyy/432cf88729cd230fb0754b8d5b958653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.traits; | |
template polymorphic(alias F){ | |
auto polymorphic(T...)(T args){ | |
mixin((){ | |
import std.conv:to; | |
string o; | |
foreach(i,S;(Parameters!F)[0..T.length]){ | |
o~="args["~i.to!string~"].to!"~S.stringof~","; | |
} | |
return "return F("~o~");"; | |
}()); | |
}} | |
void foo(int,float,bool=true){} | |
alias bar=polymorphic!foo; | |
struct myint{ | |
int i; | |
int to(T:int)()=>i; | |
} | |
alias somefloat=double; | |
float to(T:float)(somefloat)=>float.init; | |
int add(int i,int j)=>i+j; | |
alias add_=polymorphic!add; | |
void main(){ | |
bar(myint(),somefloat.init); | |
import std; | |
int i=add_(myint(1),myint(2)); | |
i.writeln; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment