Last active
December 16, 2015 20:09
-
-
Save SimonRichardson/5490221 to your computer and use it in GitHub Desktop.
Be dangerous and mutate a immutable Enum in haxe.
This file contains hidden or 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 Test { | |
| static function main(){ | |
| var a = Cons(1, Nil); | |
| trace(a); | |
| trace(ListLens.mutate(a, 2)); | |
| trace(a); | |
| /* | |
| Output from the above traces - this is dangerous ;-) | |
| Test.hx:4: Cons(1,Nil) | |
| Test.hx:5: Cons(2,Nil) | |
| Test.hx:6: Cons(2,Nil) | |
| */ | |
| } | |
| } | |
| enum List<T> { | |
| Nil; | |
| Cons(head : T, tail : List<T>); | |
| } | |
| class ListLens { | |
| public static function mutate<T>(list : List<T>, value : T) : List<T> { | |
| #if js | |
| untyped __js__('list[1] = value'); | |
| #elseif flash | |
| untyped list.params[0] = value; | |
| #elseif neko | |
| untyped list.args[0] = value; | |
| #end | |
| return list; | |
| } | |
| } |
Author
Author
Adding Neko to ListLens
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding AVM2 to ListLens