Skip to content

Instantly share code, notes, and snippets.

@anissen
Created December 9, 2015 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anissen/d7f74997146270500439 to your computer and use it in GitHub Desktop.
Save anissen/d7f74997146270500439 to your computer and use it in GitHub Desktop.
Using abstract to creating immutable objects in Haxe
class Foo {
public function new( val ) x = val;
public var x : Int;
}
abstract ConstFoo(Foo) from Foo {
public var x(get, never) : Int;
function get_x() return this.x;
}
class Test {
static function main() {
var foo = new Foo(41);
foo.x = 42;
trace(foo.x); // 42
var const_foo :ConstFoo = foo;
//const_foo.x = 43; // error
trace(const_foo.x); // 42
// *Can* still be bypassed
(cast const_foo).x = 123;
trace(const_foo.x); // 123
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment