lua-users home
lua-l archive

Re: Explanation needed for setfenv replacement for Lua 5.2

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


| lib.Interpreter( function(_ENV)
| AddItem "hello!"
| AddItem(AN_UPVALUE)
| AddItem "Hope You Enjoy!"
|
| PrintResult() --> hello! This is a silly DSL! Hope You Enjoy!
| end)
 
> It is still too boilerplate IMO when explaining how to write a DSL 
> snippet to a non-programmer
Just an idea: How about misusing the for statement?
| for _ENV in lib.Interpreter do
| AddItem "hello!"
| AddItem(AN_UPVALUE)
| AddItem "Hope You Enjoy!"
|
| PrintResult() --> hello! This is a silly DSL! Hope You Enjoy!
| end
Still the _ENV but it's readable ;-)
Full code below:
------------------------------------------------------
function Interpreter(_, second_iteration)
 if second_iteration then return end
 local private_data = {}
 return {
 AddItem = function(item) table.insert(private_data, item) end,
 PrintResult = function() print(table.concat(private_data, " ")) end,
 }
end
-- client code
local AN_UPVALUE = "This is a silly DSL!"
for _ENV in Interpreter do
 AddItem "hello!"
 AddItem(AN_UPVALUE)
 AddItem "Hope You Enjoy!"
 PrintResult() --> hello! This is a silly DSL! Hope You Enjoy!
end
------------------------------------------------------
Ciao, ET.

AltStyle によって変換されたページ (->オリジナル) /