Skip to content

Instantly share code, notes, and snippets.

@od0x0
Created July 28, 2010 19:41
Show Gist options
  • Save od0x0/495975 to your computer and use it in GitHub Desktop.
Save od0x0/495975 to your computer and use it in GitHub Desktop.
Vec3: inline cover<T>{
x, y, z: T
init: func@(=x, =y, =z)
plus: func(v: This) -> This{
new(x + v x, y + v y, z+ v z)
}
print: func{
printf("Vec3("(%s, %s, %s)\n", x as String, y as String, z as String)
}
}
v:=Vec3<Float> new(0,0,0)
v plus(Vec3<Float> new(1,0,1)) print()
//Vec3 is not actually a true type, it is literally being inlined (as a generated mangled named (like anonymous functions are) structure)
//This would be the equivalent C (more or less)
struct Vec3inlinedfloat{
float x, y, z;
}
static inline Vec3inlinedfloat Vec3inlinedfloat_plus(Vec3inlinedfloat this,Vec3inlinedfloat v){
Vec3inlinedfloat temp;
temp.x=v.x+this.x;
....
return temp;
}
static inline void Vec3inlinedfloat_print(Vec3inlinedfloat this){
printf("Vec3("(%s, %s, %s)\n", Float_OP_as_String(x), Float_OP_as_String(y), Float_OP_as_String(z));
}
Vec3inlinedfloat v=((Vec3inlinedfloat){.x=0,.y=0,.z=0});
Vec3inlinedfloat_print(Vec3inlinedfloat_plus(v,((Vec3inlinedfloat){.x=0,.y=0,.z=0})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment