Reputation: 31830
I'm starting to learn Haxe, and I want to know whether Haxe is strongly or weakly typed. If its type system isn't strong or weak, then is it "optionally typed", like the Dart programming language?
I noticed that this line of code seemed to work, despite having no explicit type declaration: var a = [["This is a nested array"], ["This is another nested array"], "This is not a nested array" ];
Upvotes: 1
Views: 534
Reputation: 3845
It's strictly typed, and the Haxe compiler type inference does an incredible job of finding the correct type of your data.
Following the example here: http://haxe.org/ref/type_infer - try this neat trick:
var a = [["This is a nested array"], ["This is another nested array"], "This is not a nested array" ];
var b = $type(a);
It will give you a warning with the message
Warning : Array<Dynamic>
From that, we can see that the compiler correctly recognized your mix of two arrays and one string, and resolved that to an array of Dynamic, wich means that the array can basically hold any object thrown at it.
As a consequence, your variable definition
var a = [["This is..."], ["This is another..."], "This is not..." ];
is completely synonymous as the following one, including the explicit type definition:
var a:Array<Dynamic> = [["This is..."], ["This is another..."], "This is not..." ];
Upvotes: 3
Reputation: 6008
Haxe is for the most part a strictly typed language, however, it isn't as strict about it as you might expect. It is far more lenient than some other languages, and allows you to break out of the type system if necessary. And it doesn't require you to specify all of your types every time - the compiler uses type inference to 'infer' the type - in your exampe the inferred type is Array<Dynamic>
With Haxe:
The compiler will default to strongly typed. You can either supply type information (eg. var x:Array<Int>;
) or you can let the compiler infer (eg. var x = [0,1,2]
, x will be Array:Int
).
You can check the inferred type by using $type(myVariable);
This can be helpful for figuring out what is going on.
If type inference confuses you, or the compiler gets confused, it can sometimes be easier to define your types explicitly, so there is no confusion.
You can escape the type system (if it gets in the way, or if you're trying to work with a native library etc) using either Dynamic, Reflection or untyped.
Haxe's Type Checking is mostly a compile time feature. Types are checked and guaranteed to be correct when you compile your code. If you try to do weird things at runtime though, no type checking occurs and you risk breaking things.
Some of Haxe's targets are weakly typed (eg. Javascript, Neko). However, because Haxe's type checking happens at compile time, you get all the benefits of a strictly typed languages - error checking, auto completion etc. The compiled Javascript or Neko Bytecode won't know about the types, but that doesn't matter - Haxe has made sure that everything works.
To help you get your head around it, here are some links:
Upvotes: 4