Skip to content

Instantly share code, notes, and snippets.

@jcward
Last active June 19, 2022 03:09
Show Gist options
  • Save jcward/1f8eee63753b7c349c0e93e55d15bd25 to your computer and use it in GitHub Desktop.
Save jcward/1f8eee63753b7c349c0e93e55d15bd25 to your computer and use it in GitHub Desktop.
Haxe global (like) functions and variables via import
package;
class Global
{
public static function parseInt(s:String):Int { return Std.parseInt(s); }
public static function int(f:Float):Int { return Std.int(f); }
public static function is(v:Dynamic, t:Dynamic):Bool { return Std.is(v, t); }
public static function typeof(o:Dynamic):Type.ValueType { return Type.typeof(o); }
public static var DEBUG_LEVEL:Int = 3;
}
package;
// In Haxe you can't (easily?) define a global function or variable, but you can
// import static functions and vars so your syntax looks like using globals.
// e.g.
// all static members of Global are now in scope, both functions and vars
import Global.*;
class Main
{
public static function main()
{
trace("parseInt: " + parseInt("51"));
trace("5 is int? " + is(5, Int));
trace("int(Math.PI) = " + int(Math.PI) );
trace(typeof([]));
trace("DEBUG_LEVEL > 2 ? " + (DEBUG_LEVEL>2));
}
}
@sonygod
Copy link

sonygod commented Jul 6, 2018

can you general js code in haxe without use Global.parseInt ?only function parseInt(value:string)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment