Last active
December 19, 2015 08:09
-
-
Save onefifth/5924055 to your computer and use it in GitHub Desktop.
<Haxe> Crash on runtime using assignment of Dynamic to a fixed type
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 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 | |
} | |
} |
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
Should this fail as silently as it does on neko and cpp targets? Swf targets at least give a runtime error.