Last active
June 19, 2022 03:09
-
-
Save jcward/1f8eee63753b7c349c0e93e55d15bd25 to your computer and use it in GitHub Desktop.
Haxe global (like) functions and variables via import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you general js code in haxe without use Global.parseInt ?only function parseInt(value:string)?