Created
December 9, 2015 09:10
-
-
Save anissen/d7f74997146270500439 to your computer and use it in GitHub Desktop.
Using abstract to creating immutable objects in Haxe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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