lua-users home
lua-l archive

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

On 05/05/2011 0.53, E. Toernig wrote:
Just for the record:

P.S: I know that I could pass the environment as an argument to the
anonymous closure like this:
lib.Interpreter( function( env )	

     _ENV = env

That would also change the callers env!

Ouch! I knew that! I forgot the local keyword!
I was tired too! :-P

That's the problem with "toy code": you risk of simplifying too much during cut and paste! :-D

Either "local _ENV = env" or simply name the parameter _ENV:

   lib.Interpreter( function(_ENV)

Nice reduction of the boilerplate code. I really didn't think of that! Thanks! It is still too boilerplate IMO when explaining how to write a DSL snippet to a non-programmer (too easy to forget or to write in a wrong way: _ENV, ENV, __Env, who knows!), but at least it becomes viable when I write code in the DSL, which is my most common use case (As I said, I hate boilerplate :-).

It's a matter of separation of concerns: whenever possible I write a small Lua driver, then express the core logic in a DSL. Together with "thin layer" OOP I found it is really a powerful technique - leading to extremely clean code. Even months later, when you maybe forgot how the driver worked, you look at the DSL and (if it was carefully designed) you can easly tune/change the business logic without delving into the possible gory details well hidden in the driver.

And it is an affordable technique too: Lua is fantastic in this respect - a few dozens of lines and you have a nice DSL interpreter without needing to know how to write a recursive descent parser!

Ciao, ET.

thanks again!