Is my ported code correct or is a better approach?

107 views
Skip to first unread message

Marcio Andrey Oliveira

unread,
Dec 16, 2013, 2:37:11 PM12/16/13
to haxe...@googlegroups.com
Hi.

I'm porting some AS3 code to Haxe.

At first it seems to be working but I'm not sure whether I converted the code as it should be.

Could someone please help me with this?

Bellow are both AS3 and Haxe code (a little more than the point where I'm in doubt just to put it in context).

Original code:
        public static var service:Object = { connect: load_service }
       
public static var ad:Object = { showPreGameAd: showPreGameAd }
       
       
public static function showPreGameAd(options:Object=null):void {
           
if (options == null) {
                options
= {}
           
}
           
if (service.showPreGameAd != undefined) {
                service
.showPreGameAd(options)
           
} else {
               
MindJoltAPI.options = options
               
if (options["ad_started"] == null) {
                    options
["clip"].stop()
               
}
           
}
       
}

Ported code:
    public static var service:Dynamic = { connect: load_service }
   
public static var ad:Dynamic = { showPreGameAd: showPreGameAd }

   
public static function showPreGameAd(options:Dynamic=null):Void {
       
if (options == null) {
            options
= { };
       
}
       
if (service.showPreGameAd != null) {
            service
.showPreGameAd(options);
       
} else {
           
MindJoltAPI.options = options;
           
var ad_started = Reflect.field (options, "ad_started");

           
if (null == ad_started) {
               
var clip_stop = Reflect.field (options, "clip");
                clip_stop
.stop();
           
}
       
}
   
}


The followin code
    MindJoltAPI.options = options
   
if (options["ad_started"] == null) {
   
    options["clip"].stop()
    }

was translated to this

    MindJoltAPI.options = options;
   
var ad_started = Reflect.field (options, "ad_started");

   
if (null == ad_started) {
   
    var clip_stop = Reflect.field (options, "clip");
        clip_stop
.stop();
    }

Did I made the correct conversion? Is there a better approach?

Thank you all.

Juraj Kirchheim

unread,
Dec 16, 2013, 3:04:41 PM12/16/13
to haxe...@googlegroups.com
The code is correct, but it doesn't really use any of the advantages
Haxe has over AS3. Also you can actually just write
`options.ad_started` if `options` is `Dynamic`.

Most importantly, anonymous objects in Haxe can be strictly typed.
It's not easy to show you this without seeing the whole code, but this
should give you an idea:

public static function showPreGameAd(?options:{?ad_started:Bool,
?clip:MovieClip}):Void {
if (options == null) {
options = { };
}
if (service.showPreGameAd != null) {
service.showPreGameAd(options);
} else {
MindJoltAPI.options = options;
if (options.ad_started) {
options.clip.stop();
}
}
}

While `*` and `Object` are common place in AS3, in Haxe `Dynamic` is
usually a bad sign.
In some cases you will also want to use enums instead of anonymous objects.

Regards,
Juraj

Marcio Andrey Oliveira

unread,
Dec 16, 2013, 7:44:43 PM12/16/13
to haxe...@googlegroups.com
Thank you very much for taking your time to help me.

I really appreciate that.

I'll study more about Haxe language in order to use it better than now.

Thank you again.

Cambiata

unread,
Dec 17, 2013, 6:15:30 AM12/17/13
to haxe...@googlegroups.com
@Marcio:

Even if it's work in progress, and not "official" yet, I would recommend Simon Krajewski's great work on coming Haxe documentation (in case you haven't seen it).
There's a pdf version at https://github.com/Simn/HaxeManual/tree/master (HaxeDoc.pdf)

Jonas

Marcio Andrey Oliveira

unread,
Dec 17, 2013, 7:20:27 PM12/17/13
to haxe...@googlegroups.com
Hi, Jonas.

I wasn't aware about this document.

I already downloaded it and started reading it.

It's really a great work.

Thank you very much for sharing.
Reply all
Reply to author
Forward
0 new messages