lua-users home
lua-l archive

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

On Fri, Apr 14, 2017 at 12:34 luciano de souza <> wrote:
If interfaces doesn't make sense in Lua, so I really can follow another way.
Actually, I feel myself a little bit lost. In other languages, we can
find many references talking about OOP, abstract classes, interfaces,
dependence injection...
With the GOF design patterns, we can assimilate several conceptual
structures and apply it.
The problem is that Lua is so different that I am not able to
understand whats the best practices.
Does we have example of design patterns in Lua? Does GOF patterns make
sense in Lua?
The reason I am using Penlight as an OOP module, even though the
metaprograming features, indicates that I am not confortable with this
lack of references.
Penlight is a very good library and allows me to think as I thought
before Lua. In the future, perhaps my mind changes, but the truth is
that Penlight allows me not to change everything.
Using Penlight, I can implement Visitor, Strategy, Abstract Factory...
If this is not natural for LUa, what's is natural? How can I find
models to follow?


I don't know that I can answer the question in the way that you might want or expect, but my answer is that Lua is very different, as you say. It's extremely simple and does not favor one style of programming over another, accepting small nods like the colon operator. 

Once I was talking to a C# programmer about some problem I was trying to solve; I think I was trying to implement some aspect of OOP that was very basic. 

I'm not a programmer by education and he is. It took him a while to understand what I was asking because "good object oriented design" had been drilled into him from day one and from every direction: his professors, the books he read, his coworkers and the language he was using. It had not occurred to him that there was another way to program, although of course he knew that there was. The lazy model that he admitted to having was some sort of hierarchy like: first came spaghetti  code, then procedures, then OOP fixed everything.

It's hard to unlearn the behaviors that the languages you're using expect from you. If you want to make programs in Lua, it's better to think in Lua than it is to try to foist concepts from other languages into it. 

Another example came from working with a long time systems level C++ programmer who was helping with the Lua C API. He was fighting with Lua tooth and nail. One day I tried to test for the existence of a field on userdata that I had gotten from him. Whenever I indexed a nil value, the program would fail an ASSERT and crash out. He had manually trapped any attempts to access nonexistent values. He did this because the Lua C API hadn't done it for him and in his mind, he was covering his bases. I had to spend some serious energy and time to explain to him why the ability to attempt to access a nonexistent table/userdata value was a useful feature and not lazy, bad practice. 

So, I guess I'm saying that I recognize your concern as something that Is similar to past experiences and that your disorientation is not unique and will not be as well served by bolting features on to Lua as it would be by learning more about how problems can be solved (and scaled) with what is there and how what is there is composed. In the end, I think that you will find that Lua will greatly enhance your perspective and you will want to solve many problems with it, in the future. 

2017-04-14 13:50 GMT-03:00, Andrew Starks <>:
> I've never been able to reason about the value or utility of most patterns,
> within the context of Lua, which is a dynamic language.
> For example: my understanding is that interfaces are a way to say that a
> particular object has all of the methods and properties that are defined by
> an interface. In a dynamic language, you can simply say that it does (if
> then) and then support those methods---if you don't it
> won't work so you need to test.
> Building up checks for compliance on a language level is probably possible
> (to some degree less than 100%), but it doesn't jive with Lua, in my
> experience. In order to enforce enough limitations to make such a concept
> useful, you have to build up type checking, etc. Eventually there is a
> point where the complexity and performance trade offs lead me to understand
> why such a thing didn't exist in the first place.
> That is, whenever I found myself meta-programming in Lua, I end up throwing
> it away.
> My .02 cents. I suspect that you won't find this helpful, but I've tried to
> implement interfaces before and your question reminded me of that
> experience.
> -Andrew
> On Fri, Apr 14, 2017 at 10:22 luciano de souza <> wrote:
>> Hello all,
>> Dispite of the fact Lua is not a object oriented language, by means of
>> modules like Penlight, we have support.
>> However, some design patterns like Strategy, Abstract Factory or
>> Visitor demands, as far as I able to know, abstract classes or
>> interfaces.
>> So my question is: is there Lua implementations for abstract classes
>> or interfaces?
>> More broadly, I ask: does someone know examples of design pattern
>> implementation in Lua?
>> Best regards,
>> --
>> Luciano de Souza

Luciano de Souza