Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active September 21, 2016 14:36
Show Gist options
  • Select an option

  • Save alphaKAI/e08a73abfc38801ebf272ae396ff4064 to your computer and use it in GitHub Desktop.

Select an option

Save alphaKAI/e08a73abfc38801ebf272ae396ff4064 to your computer and use it in GitHub Desktop.
An example of type level stack structure in D.
import std.stdio,
std.traits;
class Type(_type) { alias type = _type; }
class Holder(Types...) {
alias Types Holder;
alias types = Types;
}
class TypeStack(stk) {
bool empty() { return is (stk == Holder!()); }
auto push(type)() { return new TypeStack!(Holder!(stk.types, Type!type)); }
auto pop() {
if (this.empty) { throw new Error("TypeStack"); }
static if (stk.types.length > 1) {
return new Holder!(stk.types[0], TypeStack!(Holder!(stk.types[1..$])));
}
}
}
auto typeStack() { return new TypeStack!(Holder!()); }
void main() {
auto stk = typeStack;
writeln("stk: ", stk);
writeln("stk.empty?: ", stk.empty);
auto stk2 = stk
.push!int
.push!char
.push!bool
.push!string
.push!float
.push!byte;
writeln("stk2:", TemplateArgsOf!(typeof(stk2)).stringof);
writeln("stk2.empty?: ", stk2.empty);
auto poped = stk2.pop;
alias popedType = poped.types[0];
alias stk3Type = poped.types[1];
auto stk3 = new stk3Type;
writeln("popedType: ", popedType.type.stringof);
writeln("stk3: ", TemplateArgsOf!(typeof(stk3)).stringof);
writeln("stk3.empty?: ", stk3.empty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment