Help in porting AS3 codebase to Haxe

Hi, long time AS3 dev here, and I’m just jumping into Haxe because its faster, better (strongly typed, extension methods, etc) and it holds much more promise than AS3 or ASC2.

I need some help in porting the following Array commands:

  • array.concat() - typically used to shallow clone an array if called with no arguments, but no longer possible (at least 1 arg needed). How do I shallow clone an array using in-built functions?

  • array.length = x - used to set the length of an array, deleting extra items or adding nulls to the end. What is the Haxe equivalent?

  • myClassName - classes no longer allow lowercase names? I really need this as I have dozens of such classes. Any way to override this error? (module name must start with an uppercase letter)

Thanks

You can find the documentation there: http://api.haxe.org/

  • For array.concat() you can do array.copy().
  • To remove elements from an array you can do array.splice(pos:Int, len:Int) (return a copy).
  • To add null elements (well null on neko, not sure how that work on c++) you can do array[x] = null.
  • Class name must start with an uppercase letter, no trick around that I think.
1 Like

Thanks ibilon!

  1. Is there no way to set the length of an array?

  2. And how do you cast to int like in AS3 int(5.2) = 5, I can do cast(x, Int) but not sure if it will have the same behavior.

Only when increasing it I think.

cast is supposed to be a safe cast, so I guess here it’d fail, you can use Std.int().

1 Like

You might also consider using openfl.Vector, which should support the length argument

Yeah, between basic types, you should use something like Std.int to perform conversion. As for other types of casting (like between MySpriteSubClass and Sprite) you can use either cast (myObject, Sprite) for a safe (but slightly slower) cast, or cast myObject to perform an unsafe cast, which has no performance penalty but may crash if it was invalid

Is this the correct way to cast values? This is not exactly casting, its more like a coercing, whereby the value is automatically converted or rounded to match the output type.

  • AS3 … Haxe
  • int(5.2)Std.int(5.2)
  • Number("5")Std.parseInt("5")
  • String(500)cast(500, String)

And what is the toString equivalent in Haxe? Is this OK? In AS3 everything that inherits from Object supports toString, why doesn’t Haxe have that?

  • AS3 : var n:Number; n.toString()
  • Haxe : var n:Float; (n + "")

And lastly, Haxe does not have a strict equality operator?

  • AS3 :
    (null == undefined) = true
    (null === undefined) = false

To convert to string you can do Std.string().

And for the strict equality operator since haxe doesn’t have undefined it doesn’t need it I guess.

1 Like

I would also consider Std.parseFloat if you expect a float value, rather than an integer, as for String casting, Haxe has an automatic toString behavior which be overridden if you define a toString function in your classes, otherwise it uses a default. Generally it’s easiest to do "hello " + world, but if it’s a single value, I use Std.string (value)

1 Like

Haxe is not dynamic on a number of targets, this is one reason why Haxe C++ beats AS3 so badly in performance testing when it uses static values (but gets much slower in dynamics, AS3 is more optimized there).

An int value has no “toString” value, but the Std.string method is designed to handle both basic types as well as classes :slight_smile:

1 Like