Skip to content

Instantly share code, notes, and snippets.

@SimonRichardson
Last active December 16, 2015 20:09
Show Gist options
  • Select an option

  • Save SimonRichardson/5490221 to your computer and use it in GitHub Desktop.

Select an option

Save SimonRichardson/5490221 to your computer and use it in GitHub Desktop.
Be dangerous and mutate a immutable Enum in haxe.
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;
}
}
@SimonRichardson
Copy link
Copy Markdown
Author

Should be rather easy to mutate other target platforms!

@SimonRichardson
Copy link
Copy Markdown
Author

Adding AVM2 to ListLens

@SimonRichardson
Copy link
Copy Markdown
Author

Adding Neko to ListLens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment