HaXe Found!
Posted by Ali Mills Tue, 08 Aug 2006 04:35:00 GMT
In comments to the last post Where’s haXe?, iongion did a great job spotting many of the ways in which haXe differs from ActionScript 2.0. Highlights of the differences in syntax follow:
package mediator.events;In haXe, source code files can begin with an optional package declaration. The files should exist in subdirectories mapping to the package declaration.
private var listeners:Array<EventListener>;HaXe uses typed arrays. In the code snippet above, the type parameter <EventListener> means that the listeners array contains objects of type EventListener.
private var eventTarget:Dynamic;HaXe uses Dynamic instead of Object to signify that a variable can be of any type.
public function new(?eventTarget:Dynamic) {Class constructors in haXe are named new instead of the name of the class being defined.
And, function parameters can be made optional by using a question mark before the parameter name. In the code above, eventTarget is an optional parameter.
if(eventTarget) this.eventTarget = eventTarget else this.eventTarget = this; HaXe doesn’t have a ternary operator. Instead of using the syntax this.eventTarget = (eventTarget) ? eventTarget : this; use the if syntax above.
public function addEventListener(type:EventType, handler:Dynamic, context:Dynamic):Void {HaXe has enums which are containers for fixed numbers of values. They’re ideal when you want to ensure that only certain values are used (for example, constant values for event types). Enums are defined in files that conain other classes and possibly other enums. To use them, the file in which the enum is declared must first be imported, or you can use the full type path to access constructors as if they were static fields of the enum type. The code above uses the enum EventType which is defined in the mediator.events.Event class. That class looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
By importing the above class with the statement:
import mediator.events.Event;we have access to the EventType enum.
Also, notice the declaration of the class variables type, target, and bubbles in the Event class definition above. They’ve been declared as read-only with haXe’s getter/setter declaration style of (getter, setter); where the combination (default, null) signifies read-only.
for(i in 0...listeners.length) {
if(listeners[i].equals(listener)) {
return listeners.splice(i, 1)[0];
}
}Since haXe uses iterators, for loops look a little differently than they do in other languages. In the above code, an IntIter (int iterator) is built with the ... (three dots) operator and used to iterate through the elements of the listeners array.
public function dispatchEvent(event:Event):Bool {In haXe, the Boolean type is Bool.
untyped {event["target"] = eventTarget;}To avoid type-checking for a block of code in haXe, wrap the block of code in untyped{}.
Reflect.callMethod(listener.context, listener.handler, [event]);There is no Function class in haXe, and the Function object’s call() and apply() are replaced by Reflect.callMethod.












