5 ms·
The Power of Lua and Mixins
- AdrianRossouw 13y agoi do love lua, although the most I've really gotten to play with it was programming drones (called turtles) in the computercraft mod for minecraft.
- sieisteinmodel 13y agoAnimated pictures of a Jump'n'Run game make blog posts really hard to read. Might be related to the way human attention works, but well.
- AdrianRossouw 13y agoi thought they made the abstract nature of a construct like mixins into something far more concrete.
- agumonkey 13y agoIt does but it's unrelated, I think @sieisteinmodel meant it was far too distracting.
- kzrdude 13y agoIt's not unrelated, there's a hammer animation after the Hammer class, and dust after Dust.
- sieisteinmodel 13y agoI did not even get to that because I could not concentrate.
- Udo 13y agoI've bee studying Lua very thoroughly recently because I'm working on a toy language based on its runtime, so I gained a certain appreciation for some of the concepts Lua uses. Given this background and my general coding experience up to now, I'm very skeptical about OO frameworks for Lua, because they mostly provide mechanisms that are neither true to the language's design nor are they really necessary. Specifically, mix-in patterns are extremely easy to implement just using core features already present. Most languages work best if you don't try to forcefully adapt them to emulate another language. In my opinion, some of the worst features in both PHP and JavaScript come from a more or less hidden desire to mimic Java instead of being confident in the paradigms those languages already provide. Lua already allows you to share code between "objects", and it allows you to do that with excellent granularity, simplicity, and a lot of control. There is no question that Lua is well-suited for modification and can be adapted for many things, but in this case I'm having trouble seeing the benefit (other than perhaps a programmer's familiarity with "standard" OO design which can be a considerable boon in the short term).
- justincormack 13y agoMixins are a useful mental model for some kinds of code reuse though. (I don't like his class style either, the constructor should return self, and I just like prototypal inheritance anyway).
- Udo 13y ago> Mixins are a useful mental model for some kinds of code reuse though. They are, but I can think of at least one core feature-based solution to do the same thing with less overhead. Each mix-in or trait could easily be in its own table, to be joined with the object as it's being constructed in whatever fashion desirable.
- exizt88 13y agoNot bashing PHP in any way (I'm a PHP developer myself), but what would you consider the PHP paradigm?
- Udo 13y agoI like PHP as well, however it's the wrong language to build byzantine OO inheritance structures in. Yet, that's what new feature development seems primarily directed at: new OO functionality that adds a lot of complexity and new opportunities for developers to shoot themselves in the foot. Listening to PHP core developers' presentations, I get the impression they're trying to imitate Java. The per-request execution model can be played as an incredible strength, but the moment some framework comes along, starting off by loading all of its code, then instantiating a myriad classes, subclasses, traits, and other crap just to serve a simple request - that's where slowness and bugginess comes from. I think simple objects do work well in PHP, together with judicious use of the powerful array data structure, anonymous functions, and on-demand code loading.
- exizt88 13y ago> The per-request execution model can be played as an incredible strength, but the moment some framework comes along, starting off by loading all of its code, then instantiating a myriad classes, subclasses, traits, and other crap just to serve a simple request - that's where slowness and bugginess comes from. Really good point, never thought about it this way.
- kephra 13y agoIt would be better if this article had been using real Lua code, and not starting with a "class" function. Lua does !NOT! have classes. Lua is a prototyping language, and anybody can handwave an own class system, or go without classes at all. The idea of mixins makes only sense in languages that have classes. Lua does not. References have a metatable, and this metatable contains functions. The functions are independent of the reference, and there are no classes. So basically all functions in a metatable are mixins by default.
- bartbes 13y agoYet the very first sentence of the article mentions the class library he's using for the rest of the it.
- octopus 13y agoBest Lua "class" example I've seen is in the PIL 3rd edition it is about 10 lines of code and lets you use inheritance and some other nice OOP concepts.
- kzrdude 13y agoThis is a boring view! All the same concepts of classes, interfaces, objects still exist when using Lua, you just don't have a canonical implementation. This is also interesting in itself -- the best class system may spread and be improved.
- otikik 13y agoHi, I am the author of the library mentioned in the article (not the article author, that's someone else). You seem to have missed the link to the OOP library used, so here it is: https://github.com/kikito/middleclass https://github.com/kikito/middleclass Also, notice that in other comments on this thread I mention that I don't think OOP in Lua it's a one-size-fits-all thing. I think middleclass has its uses, but most of the time plain Lua with some metatables does the trick just fine.
- veb 13y agoWait. Just having a glance at the website, and I see these animations - do they have anything to do with the code? It doesn't quite clearly state that and I'm kind of put off reading the code. :-/ Sorry if I've missed something.
- mantrax4 13y agoLua's most popular use is for scripting game engines. From simple side scrollers to powerful FPS 3D engines. So it's basically like "a taste of the world of Lua" or something like this, to make the presentation less dry.
- yp_master 13y ago1 notmagi.me: 55 bytes, 1+1+0+1 records, response, authoritative, noerror query: 1 notmagi.me answer: notmagi.me 1800 A 207.97.227.245 additional: . 32768 weird class Nice.
- otikik 13y agoHi there, Thanks for writing this article. I believe we spoke about this very subject in reddit not long ago. In that conversation I mentioned that I'd rather use composition instead of mixins for most of the examples you made. Rather than repeating myself, here's the link, in case someone wants to see an alternative approach: http://www.reddit.com/r/gamedev/comments/1uni58/the_power_of_lua_and_mixins/ http://www.reddit.com/r/gamedev/comments/1uni58/the_power_of...
- kaonashi 13y agoI've always found mixins in other languages to be absolutely horrible in terms of maintainability. They often lead to all sorts of sins and junk-drawer design. I'd prefer to see something like a clojure-ish protocol library.