Skip to content

Instantly share code, notes, and snippets.

@onefifth
Last active December 19, 2015 08:09
Show Gist options
  • Save onefifth/5924055 to your computer and use it in GitHub Desktop.
Save onefifth/5924055 to your computer and use it in GitHub Desktop.
<Haxe> Crash on runtime using assignment of Dynamic to a fixed type
class Main {
static function main(){
var aString : Dynamic = "a string";
var anArrayOfStrings : Dynamic = ["array", "of", "strings"];
var correctlyTypedVar:Array<String> = anArrayOfStrings;
var incorrectlyTypedVar:Array<String> = aString;
//Type.typeof(correctlyTypedVar) // This is an Array at runtime
//Type.typeof(incorrectlyTypedVar) // This is a String at runtime
//trace (correctlyTypedVar.length); // 3
//trace (incorrectlyTypedVar.length); // 8
for (i in correctlyTypedVar) { // Outputs the array values as expected
trace (i);
}
for (i in incorrectlyTypedVar) { // This crashes silently cuz strings do not have iterators
trace (i);
}
trace ( "Finished" ); // This never happens
}
}
@back2dos
Copy link

back2dos commented Jul 8, 2013

When using Dynamic, behavior is undefined, i.e. it will depend on the target platform.

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