Closure vs Callback
In mobl you have Callback {} with the only input I think “event” like this:
var cb : Callback = {
alert(event);
};cb(“simple test”);
In languages like Ruby you have also named parameters like:
{ |a,b| print a,b }Is it planned something more like this in mobl? Or is it possible to set other variables than “event”?
Submitted by Terje Pedersen on 28 April 2011 at 09:57
Issue Log
This is indeed planned: https://yellowgrass.org/issue/mobl/70. Any feedback on syntax is welcome, however I would prefer to keep it close to Javascript.
Good question about syntax, I think I need to think about it :)
javascript:
function(a,b) {code}ruby:
{|a,b| code}groovy:
{a,b -> code}python:
lambda a,b: code
Some examples of how it could look like.
function(a:Num, b:String) {code}
{|a:Num, b:String| code}
{a:Num,b:String -> code}
{a:Num,b:String => code}
{a:Num,b:String :: code}Test example:
var ar = [1, 2, 3, 4];
ar.filter({n : Num, Bool -> return n % 2 == 0; });
Implemented this both for anonymous controls and functions in 0.4.4. Syntax:
var fn = function(a : Num) : Num { return a + 10; }; var ctrl = control(a : Num) { label(a + 10) };
This is working:
function xy(it : Num) : [Num] {
return [it, 2000];
}function accountgroup() : [Num] {
var r = range(0,13);
return [r.map(xy)];
}But with the new anonymous function I get errors:
function accountgroup() : [Num] {
var r = range(0,13);
var xy = function(it : Num) : [Num] { return [it, 2000]; };
return [r.map(xy)];
}And while typing I know why, the compiler thinks the return inside the anonymous function is the return of accountgroup()
Log in to post comments