Skip to content

Instantly share code, notes, and snippets.

@grogancolin
Last active August 30, 2016 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grogancolin/066a8a8c105fa473dfee961e2481a30e to your computer and use it in GitHub Desktop.
Save grogancolin/066a8a8c105fa473dfee961e2481a30e to your computer and use it in GitHub Desktop.
void main(){
import std.stdio;
auto nodeI = new Node!(int)([1,2,3]);
auto nodeF = new Node!(float)([1f,2f,3f]); /+ fails with error:
$ dmd test.d
test.d(26): Error: function literal __lambda4 (int t) is not callable using argument types (float)
/Library/D/dmd/src/phobos/std/algorithm/iteration.d(480): instantiated from here: MapResult!(__lambda1, float[])
test.d(26): instantiated from here: map!(float[])
test.d(4): instantiated from here: Node!(float, __lambda4)
+/
auto nodeF2 = new Node!(float, (float f) => f*f)([1f,2f,3f]); // works
writeln(nodeI.test);
writeln(nodeF.test);
}
template Node(T, alias func=(T t) => t*t){
// Predefining the alias function seems to cause hassle with nodeF above
class Node{
T[] items;
Node[] children;
this(T[] ts){
items = ts[];
}
static if(func is null){
auto test(){
return items;
}
}
static if(func !is null){
auto test(){
import std.algorithm;
return items.map!(a => func(a));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment