Support "as" and "is" as first class operators

Are there any plans to support “as” and “is” as first class operators like all the modern programming languages? (ie. AS3, C#, etc)

Currently we have cast(x, Sprite) and Std.is(x, Sprite) but these are somewhat clunky compared to the much easier to read syntax that x as Sprite allows.

This is a Haxe issue, not an OpenFL one.

Last I heard the haxe team doesn’t plan on adding any more keywords.

1 Like

If you know you can do an unsafe cast, then instead of cast (instance, Sprite) you can use cast instance which should feel more compact.

I believe some of the concept behind it is that runtime methods (such as checking the type of the object) should translate cleanly from the Haxe code to the target language. Not all languages support an “is” keyword, or work the way a Haxe “is” check would. It also allows for overrides, which is nice, such was the case recently when we overrode the behavior of Std.is in order to better support certain versions of Safari. While we wait for the next release of Haxe (which includes the patch) we can include the improvements, which I think a static keyword would not allow.

I suppose I can say as well, that when I first came to Haxe, this felt a little clunky. Now I prefer the differences between the two cast types (and enjoy being able to just put in “cast” rather than naming the type, like when passing an argument into a function, foo (cast param);) and I don’t hardly use Std.is, just the way I write my code now, given Haxe’s slant toward type safety

You can also create your own library functions for these and get at them easier with the ‘using’ keyword. For instance:

public static inline function as<T>( obj:Dynamic, type:Class<T> ):T {
   return Std.is( obj, type ) ? cast obj : null;
}

I think I did that right. I understand the handiness of as in flash, since it returns null instead of an error. Then you can do:

using MyLibraryClass; //in header right after imports
myVar = unknownVar.as( Int ); //in class code, every object will be given .as()
1 Like