Created
January 18, 2011 01:12
-
-
Save gcharnock/783832 to your computer and use it in GitHub Desktop.
Elegant way to walk a tree at compile time - that doesn't work
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.typetuple; | |
template staticReduce(alias F,T...) { | |
static if(T.length==0) { | |
alias TypeTuple!() staticReduce; | |
} else static if(T.length==1) { | |
enum staticReduce=T[0]; | |
} else { | |
enum staticReduce=staticReduce!(F,TypeTuple!(F!(T[0],T[1]),T[2..$])); | |
} | |
} | |
template concan(string s1,string s2) {enum concan = s1 ~ s2;}; | |
static assert(staticReduce!(concan,"Hello", " world")=="Hello world"); //passes | |
template Flatten(Tree,string s = "") { | |
alias staticReduce!(concan,staticMap!(Flatten,Tree.ChildrenTuple)) Flatten; | |
} | |
struct TreeNode(Children...) { | |
alias Children ChildrenTuple; | |
} | |
alias TreeNode!("A",TreeNode!("B","C","D"),"E") StaticTree; | |
static assert(Flatten!(StaticTree) == "ABCDE"); | |
//Error: alias static_walk_tree.Flatten!(TreeNode!("A",TreeNode!("B","C","D"),"E")).Flatten recursive alias declaration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment