Pretty excited to see that mx2 appears to support closures!
Is the following code valid/safe in mx2?
What are the consequences/penalties/performance-issues by doing this? For example, what is happening (internally) to name:String once CreateItem has returned?
Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FunctionMain:Void()
Localitem1:=CreateItem("closure1")
Localitem2:=CreateItem("closure2")
item1.Run()
item2.Run()
End
FunctionCreateItem:Item(name:String)
Localitem:=NewItem
item.Callback=Lambda()
Print"closure:"+name'this is reading name from the closure above it
Yep, that’s fine. I use stuff like this all over the place in Ted2 , eg: mojox.Action.
name:String is ‘captured’ by the lamba, which effectively means it’s copied into a private area of the lamba. But since strings are immutable and reference counted, this is very cheap.
That is good news! Might be worth adding an example to the Lambda docs. I was going to do it in the online Manual, but it looks like comments are only available for modules?