Assertions for compiler type checks

54 views
Skip to first unread message

sano98

unread,
Nov 12, 2014, 5:09:29 AM11/12/14
to haxe...@googlegroups.com
Hey there,

I have stumbled upon this problem several times now without finding a satisfying solution:

There is a superclass Fruit and a class Banana which inherits from Fruit. Banana has the function peel(), which the superclass Fruit does not have.

I now have an fruitArray:array<Fruit>, from which I know that only bananas will be added to.

When trying to call the peel()-method on the array-members, the compiler will complain that fruits don't have the peel()-method. And he is right doing so!


What I'm now trying to do is:

if (Type.getClass(fruitArray[i]) == Banana)
{
     fruitArray[i].peel()
}

...but the compiler remains stubborn.

I know I could make the FruitArray a BananaArray or add the peel()-method to the Fruit-class, but that's not what I want.
I'm modifying structures of an existing framework, where limiting the capabilities of the FruitArray to a BananaArray is not an option, while cluttering the superclass with virtual functions seems terribly inefficient.

Is there a way to tell the compiler: "Hey, buddy, I know you're a little worried about the Fruit class not having the peel()-method, but relax - I can assure you that only peelable bananas will be on this party, although it is generally open to any kind of fruit."

Thanks in advance,


-sano98

Juraj Kirchheim

unread,
Nov 12, 2014, 6:28:01 AM11/12/14
to haxe...@googlegroups.com
Try this:

  if (Std.is(fruitArray[i], Banana))
  {
     cast(fruitArray[i], Banana).peel();
  }

Alternatively, if you know this to hold for the whole array, this might help:

   var bananaArray:Array<Banana> = cast fruitArray;

More info here (note that the actual information is in the subsections 5.23.1 & 5.23.2): http://haxe.org/manual/expression-cast.html

Best,
Juraj

--
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.

Drakim

unread,
Nov 12, 2014, 7:18:16 AM11/12/14
to haxe...@googlegroups.com


I believe this way should be very fast (if unsafe). If you are absolutely sure that fruitArray only contains bananas you don't even need the Type.getClass check.


if (Type.getClass(fruitArray[i]) == Banana)
{

     
var myBanana:Banana = cast fruitArray[i];
    
myBanana.peel()
}

sano98

unread,
Nov 12, 2014, 8:18:28 AM11/12/14
to haxe...@googlegroups.com
Hey Juraj,

casting does the magic! Works perfectly, thanks.
Actually, now that I know where to look it up, it even says so in the documentation:

Unsafe casts are useful to subvert the type system.

=) Exactly what I was looking for.

Best,
-sano98

sano98

unread,
Nov 12, 2014, 8:21:16 AM11/12/14
to haxe...@googlegroups.com
Ah, you're right, I can even skip the type check if I'm sure about the content of the array. Thanks!
Reply all
Reply to author
Forward
0 new messages