Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created February 15, 2014 15:38
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 slaskis/9020959 to your computer and use it in GitHub Desktop.
Save slaskis/9020959 to your computer and use it in GitHub Desktop.
A quick test with abstract types in haxe. This is really cool
abstract AVector(Array<Float>) {
public var x(get, never):Float;
public var y(get, never):Float;
public var z(get, never):Float;
public var length(get, never):Float;
public var lengthSq(get, never):Float;
public inline function new(x:Float, y:Float, z:Float=0.) this = [x, y, z];
@:op(A+B) public static inline function add(a : AVector, b : AVector) : AVector {
return new AVector(a.x+b.x,a.y+b.y,a.z+b.z);
}
@:op(A-B) public static inline function sub(a : AVector, b : AVector) : AVector {
return new AVector(a.x-b.x,a.y-b.y,a.z-b.z);
}
@:op(A*B) public static inline function mulVector(a : AVector, b : AVector) : AVector {
return new AVector(a.x*b.x,a.y*b.y,a.z*b.z);
}
@:op(A*B) public static inline function mulInteger(a : AVector, b : Int) : AVector {
return new AVector(a.x*b,a.y*b,a.z*b);
}
inline function get_x():Float return this[0];
inline function get_y():Float return this[1];
inline function get_z():Float return this[2];
function get_lengthSq():Float return this[0]*this[0] + this[1]*this[1] + this[2]*this[2];
function get_length():Float return Math.sqrt(this[0]*this[0] + this[1]*this[1] + this[2]*this[2]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment