single line unsafe cast for all platform?

154 views
Skip to first unread message

AG-w

unread,
Feb 25, 2015, 12:30:35 AM2/25/15
to haxe...@googlegroups.com
I saw an article saying how to write unsafe cast in single line
http://blog.zame-dev.org/tricky-unsafe-cast-is-too-tricky/

but apparently It's not working on all platform
is It bugged or intended?

It's nice that you can safe cast in single line
but you need to cast it to another variable to use unsafe cast 

Jonas Malaco Filho

unread,
Feb 25, 2015, 4:38:49 PM2/25/15
to haxe...@googlegroups.com
Assuming you'll be using this for down casting (you lost type information somewhere, and now want to go back from a base class to the particular sub class you know the object is), there is `Std.instance`.

The behavior of an unsafe cast + type check is indeed unique in hxcpp: no cast is generated whatsoever (most likely because the type check suggested that type was already unifiable, even though in this case that's not true in the target).

AG-w

unread,
Feb 25, 2015, 10:57:12 PM2/25/15
to haxe...@googlegroups.com
what's the difference between cast and Std.instance?
does it have better performance?

Nicolas Cannasse

unread,
Feb 26, 2015, 3:17:35 AM2/26/15
to haxe...@googlegroups.com
Le 26/02/2015 04:57, AG-w a écrit :
> what's the difference between cast and Std.instance?
> does it have better performance?

If the type does not match, cast will throw an error, Std.instance will
return null. Std.instance is usually more efficient if you want to deal
with error recovery. Should better than a Std.is + cast.

Best,
Nicolas

Viachaslau Tratsiak

unread,
Feb 27, 2015, 2:30:25 AM2/27/15
to haxe...@googlegroups.com
Unsafe cast generates less code comparing to Std.instance():

haxe - Std.instance(a, B).foo();
js - ((a instanceof B) ? a : null).foo();
cpp - ::Std_obj::instance(a, hx::ClassOf<::B>())->__Field(HX_CSTRING("foo"), true)();
conclusion - :(         

haxe - var b:B = cast a; b.foo();
js - var b = a; b.foo();
cpp - ::B b = a; b->foo();
conslusion - :)


Is there are some way to achieve the same effect as in unsafe cast, but without additional variable? Maybe some macro-magic?

Jonas Malaco Filho

unread,
Feb 27, 2015, 7:55:58 AM2/27/15
to haxe...@googlegroups.com
Fortunately I was not able to reproduce the dynamic field lookup with Std.instance and the cpp target using
recent Haxe/hxcpp development versions. This already cuts down most of the performance penalty...

        ::B tmp = ::Std_obj::instance(a,hx::ClassOf< ::B >());
        tmp->foo();


While you should probably file an issue on Haxe questioning the different behavior of casting + type checking
on the cpp target, I advise you not to dismiss the benefits of the Std.instance overhead,if you
haven't yet established it as the bottleneck.

As for some way to hide or automate the creation of a temporary variable in an unsafe cast, how about a function
like the following (adapted from Std.instance, if you're curious):

    public static inline function unsafeCast<T:{},S:T>( value : T, c : Class<S> ) : S {
            var ret:S = cast value;
            return ret;
    }


If inlined as so, it still generates an extra stack variable and assignment, but you can try to tune it more. Also,
it can easily be changed to something else if, for instance, the behavior of (cast a:B) in the cpp target changes.

    ::B tmp;
    {
        ::B ret = a;
        tmp = ret;
    }
    tmp->foo();

Coming back to Std.instance, you could make your unsafeCast function call it in debug mode.

Viachaslau Tratsiak

unread,
Feb 27, 2015, 8:17:56 AM2/27/15
to haxe...@googlegroups.com
Thanks for answer.
I definitely should try latest Haxe from development branch, because Haxe 3.1.3 doesn't inline this function for js, and generates worse code for cpp.

Andreas Mokros

unread,
Feb 27, 2015, 9:49:32 AM2/27/15
to haxe...@googlegroups.com
Hi.

On Thu, 26 Feb 2015 23:30:25 -0800 (PST)
Viachaslau Tratsiak <restor...@gmail.com> wrote:
> Is there are some way to achieve the same effect as in unsafe cast,
> but without additional variable? Maybe some macro-magic?

What I don't understand here: Why do would want an unsafe cast, when a
safe cast works fine in your example?

For (cast a:B) syntax not working on hxcpp, you should create an issue
maybe.

--
Mockey

Viachaslau Tratsiak

unread,
Feb 28, 2015, 3:14:15 AM2/28/15
to haxe...@googlegroups.com
> What I don't understand here: Why do would want an unsafe cast, when a  safe cast works fine in your example?

For performance reason. Either safe cast or Std.instance() performs additional actions.


> For (cast a:B) syntax not working on hxcpp, you should create an issue maybe.

Yep. But problem is that I want to use that feature not after month, not tomorrow, but yesterday. And I try to find any work-around.
Reply all
Reply to author
Forward
0 new messages