lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm having problems getting Lua to do something like Smalltalk/Self blocks.

As a simple example, in Self you can do something like:

   myCollection do:[ :a | a > 24 ifTrue:a = a + 1]

which adds one to each element whose value is over 24.
The bit in []s is a "block" and is essentially a function that can be passed arround
as an argument.

In Lua, it might be something like this:

   myCollection:do( function myBlock(self, a) if self.a > 24 then self.a = self.a + 1 end end )

or if 'a' is an object:

   myCollection:do( function myBlock(a) if a:isGreaterThan(24) then a:increment() end end )

or if we use fallbacks on ">", "=" and "+" to map to "isGreaterThan", "setValue"
and "add" messages:

   myCollection:do( function myBlock(a) if a > 24 then a = a + 1 end end )


But I get a syntax error when I try to declare a function within a function.
Is there any way around this syntax error problem?

Steve