Why is my messy template situation not crashing?

51 views
Skip to first unread message

Chris Burt-Brown

unread,
Nov 3, 2016, 8:06:48 PM11/3/16
to Haxe
Let's say I have the following type:

class InABox<T>
{
public function new() {}
public var t: T;
}

Oh wow, IntelliJ copies colours. Awesome. Anyway, I decide to make a bunch of these things each with a different T.

var a = new InABox<Int>();
var b = new InABox<Float>();
var c = new InABox<Bool>();
var d = new InABox<String>();

What's surprising to me is that I'm allowed to put them all in an array like so:

var list: Array<InABox<Dynamic>> = [ a, b, c, d ];

I fully expected that assignment to fail, but it doesn't. Hey cool. It does say in the documentation that type parameters are bound on instantiation, so it makes sense to me that the four InABox instances aren't necessarily of disparate types in compile time.

The thing is though, now it seems my code is in a weird place, because list[0] is statically typed to InABox<Dynamic> but the instance was actually made as a type InABox<Int>.

So the obvious next step is to assign a string to it, to watch the code crash and burn.

list[0].t = "hi there";
list[1].t = "hi there";
list[2].t = "hi there";
list[3].t = "hi there";
trace(a.t); // 0
trace(b.t); // NaN
trace(c.t); // true
trace(d.t); // "hi there"

But it doesn't crash and burn. It just sort of ... doesn't work? There's no actual error and execution continues normally. It seems to just be going with the best values it can. But I'm just not sure what is going on here. It's obvious to me that this code is Not a Good Idea. But normally assigning "hi there" to a field of type Int would not even be an allowed statement, yet in this situation it's running fine.

What are the rules in this situation and what is the mechanism behind the scenes that makes it work?

Chris Burt-Brown

unread,
Nov 3, 2016, 8:16:05 PM11/3/16
to Haxe
I forgot to mention that I'm targeting swf. (Haxe 3.2.1)

Tarwin Stroh-Spijer

unread,
Nov 3, 2016, 9:39:16 PM11/3/16
to haxe...@googlegroups.com
I think most of what Haxe does is compile time type checking. Different platforms will handle things differently of course. Flash I think would ... well ... it compiles each template to a different Class I think. The array is told it can hold anything (Dynamic) so it's fine there. When you do the assign I understand that you nay expect a compile time check, but I think because it's Dynamic you're pretty much saying "hey do no checking here". The weirdness out the other end is just Flash taking the string and trying to assign it to the actually typed vars in it's class for each time.

My guess ....



Tarwin Stroh-Spijer
_______________________

phone: +1 650 842 0920

Developer at Fanplayr Inc. (Palo Alto)
Original at Touch My Pixel (touchmypixel.com)
_______________________

On Thu, Nov 3, 2016 at 5:16 PM, Chris Burt-Brown <broke...@gmail.com> wrote:
I forgot to mention that I'm targeting swf. (Haxe 3.2.1)

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Franco Ponticelli

unread,
Nov 4, 2016, 12:18:13 AM11/4/16
to haxe...@googlegroups.com
Tarwin is right and you don't have to build a complicated example to see it:

var a: Dynamic = 1;
a = "x";

The compiler will not complain and the results will vary very much dependending on your target (in JS, things might just work). In the end, don't use Dynamic ... ever. The recently introduced `Any` is a much safer option.

Chris Burt-Brown

unread,
Nov 4, 2016, 3:44:16 AM11/4/16
to Haxe
Hmm. Any looks awesome in the help docs. But it's not available yet in the haxe.org/download . I can probably wait.

Franco Ponticelli

unread,
Nov 4, 2016, 10:15:10 AM11/4/16
to haxe...@googlegroups.com
Yes, it is a new feature in dev only.

On Fri, Nov 4, 2016 at 1:44 AM, Chris Burt-Brown <broke...@gmail.com> wrote:
Hmm. Any looks awesome in the help docs. But it's not available yet in the haxe.org/download . I can probably wait.

--

Juraj Kirchheim

unread,
Nov 4, 2016, 3:04:41 PM11/4/16
to haxe...@googlegroups.com
For the time being you can use tink_core, which is where it originally came from - or you just copy it into your code base. There's no special treatment for it, it just leverages the existing language features.

Please note though, that this still compiles: 

  var a:Any = 1;
  a = "x";

But this doesn't:

  var list: Array<InABox<Any>> = [ a, b, c, d ];

Because all the erasure magic that kicks in for Dynamic does not apply for Any, which is what the difference between the two comes down to.

Best,
Juraj
Reply all
Reply to author
Forward
0 new messages