-
Notifications
You must be signed in to change notification settings - Fork 0
Closures Syntax
Using closures in Realaxy Editor performs two missions:
Providing more compact syntax for anonymous functions. Enabling type safe behavior for functions. Syntactic Features
function ( function ( parameters ) )
Closure syntax is very concise. Parameters type declaration is usually hidden. For readability and brevity, there is a special view for single-line closures – in such a closure a semi-colon on the end is omitted. Closures have also a special behavior – “the last statement is a return value” – which means you should use “1;” (in the last line) instead of “return 1;”.
// declaring f1, f2 and f3 var f1 : {=>void}; var f2 : {int=>void}; var f3 : {String, String, int=>String};
// closure-style f1 = { => }; f2 = { p2 : int => }; f3 = { p1, p2 : String, p3 : int => "Hello" }; // type declaration // is even more compact // if you enumerate two parameters // of a single type
// the same code in a "classic" style f1 = function() : void { };
f2 = function(p1 : int) : void { };
f3 = function(p1 : String, p2 : String, p3: int) : String { return "Hello"; }; Type Safety
In the pure ActionScript3 each function can be only of a single type (also Function), no matter what value it returns, how many parameters it has or which type those parameters are.
var f : Function; // f can be of a Function type only
f = function() : void { }; // typesystem checks will not work here f = function(p1 : int) : void { }; // and you can always send a wrong parameter to a function f = function(p1 : String, p2 : int) : String {return "Hello"; }; // without any warning Closure is type safe, i.e. autocomplete and typesytem checks work here:
var f1 : {=>void}; // f1 doens't require any parameter and is void var f2 : {int=>void}; // f2 requires one integer argument and is void var f3 : {String, int=>String}; // f3 has two arguments and returns String
// using closures don't give you a chance to send a wrong parameter to a function
f1 = function() : void { }; // valid code f1 = function(a:int) : void { }; // wrong type, typesystem will report an error here f1 = function() : String { return "Hello!" }; // wrong type, typesystem will report an error here