Skip to content

Instantly share code, notes, and snippets.

@DannyArends
Created June 14, 2012 20:58
Show Gist options
  • Select an option

  • Save DannyArends/2932914 to your computer and use it in GitHub Desktop.

Select an option

Save DannyArends/2932914 to your computer and use it in GitHub Desktop.
Though about the cross
import std.stdio;
interface CrossDef{
abstract int init();
abstract int emit();
}
class RIL : CrossDef{
int init(){ return 1; }
int emit(){ return 2; }
}
class BC : CrossDef{
int init(){ return 1; }
int emit(){ return 2; }
}
class preCC : CrossDef{
this(bool phase = true){ _phase = phase; }
int init(){ if(_phase){ return 1; }else{ return 2;} }
int emit(){ return 2; }
bool _phase;
}
class Cross(T : CrossDef) : CrossDef{
this(T clazz){ _clazz = clazz; }
int init(){ return _clazz.init(); }
int emit(){ return _clazz.emit(); }
T _clazz;
}
CrossDef create_cross(string type){
switch(type){
case "BC": return(new Cross!(BC)(new BC()));
case "RIL": return(new Cross!(RIL)(new RIL()));
case "preCC_PK": return(new Cross!(preCC)(new preCC()));
case "preCC_PU": return(new Cross!(preCC)(new preCC(false)));
}
}
int hmm_fun(CrossDef c){
//Now we can call any function CrossDef has defined,
//and the compiler forces any implementation to be valid
c.init();
}
void main(){
CrossDef backcross = create_cross("BC");
CrossDef rilcross = create_cross("RIL");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment