[haXe] haXe and __global__ magic

armencho at gmail.com armencho at gmail.com
Wed Jul 15 12:27:25 CEST 2009


Flash Platform specific (mostly):

Hi all,

In haXe we have a typed "keyword" "trace" which basically maps to an
untyped call to __global__["trace"] speaking with haXe syntax terms.
This __global__ is a reference to a namespace where Flash Player
actually has a whole host of other variables, including classes you
might have:

//Valid references
__global__["uint"]; //basic type
__global__["flash.display.DisplayObject"]; //DisplayObject native
class (Class object)

etc..

One can also create their own properties, like:

__global__["my_prop"] = /* some value */;

So, I was wondering, why don't we have some more support from the
compiler to avoid going with "untyped" all the time?

I would like to create my own function, like "assert":

__global__["assert"] = function(cond: Boold, ?posInfos: haxe.PosInfos)
{
	if(!cond)
	{
		var human_readable_error_msg = "Assertion failed in file \" +
posInfos.fileName + "\", line " + posInfos.lineNumbe + ", method \" +
posInfos.className + "::" + posInfos.methodName + "\n\tContext: " +
posInfos.customParams;
		
		throw new flash.Error(human_readable_error_msg);
	}
};

then I can put "assert(typeof(world) == "Sphere")" and it would be
perfectly valid call in AVM2. Currently if I want to call such a
global function I need to type "untyped __global__["assert"]" and it
is also undocumented so may break my code in the future.

Of course I can put this function into my own Debugger or Assert or
Utils class, but I just hate hate hate extra typing where no extra
typing should be necessary. Since we are typing in code manually, at
least most of us, and our IDEs are not intelligent enough for the most
part to perhaps type in  "Assert.assert" when we type "assert", a
compiler that understands such syntax can be useful, especially when
the platform supports it.

Since this may turn out to be impossible to do for other platforms, we
can have a flash.AVM2 class with a setGlobalObjectField(field: String,
value: Dynamic) function or something.

Since haXe is so good with optimized code, why not go the extra step.

Humbly yours,
-armencho at gmail.com :-)



More information about the Haxe mailing list