Created
July 5, 2011 20:42
-
-
Save francescoagati/1065871 to your computer and use it in GitHub Desktop.
Why haxe is a better php (1) - mixin - annotations - array deferencing - closure_and_chaining_function_call
This file contains 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
//support for annotations | |
// https://wiki.php.net/rfc/annotations | |
@class_annotation({a:1,b:2}) | |
class Main { | |
@range(1,100) | |
public static var value:Int=0; | |
@before({func:'before_func'}) | |
public static function main() { | |
} | |
} |
This file contains 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
//support for array deferencing | |
// https://wiki.php.net/rfc/functionarraydereferencing | |
class Main { | |
public static function main() { | |
trace(get_fruits().a); | |
trace(get_list_numbers()[0]); | |
} | |
public static function get_fruits() { | |
return { | |
a:'apple', | |
j:'juice' | |
} | |
} | |
public static function get_list_numbers() { | |
return [1,2,3,4,5]; | |
} | |
} |
This file contains 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
//support for real closure (without explicit variable in closure) and chainable function call | |
// https://wiki.php.net/rfc/fcallfcall | |
class Main { | |
public static function main() { | |
var sum=function(a) { | |
return function(b) { | |
return function(c) { | |
return function(d) { | |
return a+b+c+d; | |
}; | |
} | |
}; | |
}; | |
trace(sum(1)(2)(3)(4)); | |
} | |
} |
This file contains 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
// Haxe support traits with using that add Methods | |
// at compile time based on the type of the first parameter of static method of class | |
// used as mixin | |
class StringMixin { | |
public static function addPPP(s:String):String { | |
return s + "ppp"; | |
} | |
} | |
//--------------------------------------------------------------------- | |
using StringTools; | |
using StringMixin; | |
class Main { | |
public static function main() { | |
var x:String="i am a string"; | |
"i am another string".addPPP().addPPP().addPPP();\ | |
x.addPPP(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment