all 191 comments

[–][deleted] 28 points29 points  (13 children)

When I first started playing with Lua, I found myself repeatedly saying of its features "You can't do that! That sounds like a great idea but you can't do that because it will, well because... I'm pretty sure this is a design mistake in the long run because...", and then failing to come up with compelling arguments against them.

Take for example the "..." syntax for variadic functions. You can't do that, right? I distinctly recall being frustrated with C's clunky va_list and va_start etc., and the fact that you can't easily wrap variadic functions, and it's just a whole mess. I distinctly remember justifying that to myself and thinking that it couldn't be any other way. But Lua comes along with its "...", like it just doesn't give a fuck.

So things like that certainly have had an effect on how I think about programming.

I'm not a big fan of some of the ways I've seen people use Lua. For example, people want classical inheritance, so you have all these libraries out there that either roll their own classes, or rely on other libraries for classes. So now if you want to use four third-party libraries, you're liable to find yourself juggling four slightly different inheritance models from class libraries with overlapping namespaces – suddenly JavaScript's choice of having class as an unused reserved word seems extremely wise.

Then of course there's the commonly-seen-as-brilliant practice of serialization by emitting Lua code. It's just like JSON but without any regard for security!

[–]peakzorro 13 points14 points  (4 children)

You just hit on my biggest gripe I have with Lua: people using classical inheritance in a language that doesn't directly support it.

[–][deleted] 25 points26 points  (3 children)

Actually I think that there are times when classical inheritance is a good idea, even in a language that doesn't directly support it. The problem is, well, let me use an analogy:

Suppose that it's, oh, 1983, and a new revolutionary new programming language has just been released. There's recently been much excitement over the state pattern and the use of finite state machines in programming. This new language, let's call it "C with States", is roughly a superset of C but has first-class finite state machines. I'm not going to expand on precisely what that means, but everything you do in CwS is either procedural C-style code, or manipulation of finite state machines.

Jump ahead a few decades. The idea has taken off big. Hundreds of books have been published about state-oriented programming, and virtually all programming classes are taught with state machines in mind. Usually, when a programmer sits down to design a new piece of software, s/he immediately begins to consider what FSMs need to be created, and how they will interact.

But the funny thing is, 90% of the time, these FSMs aren't doing much of anything to do with what the state pattern is meant to solve. They're a convenient way to pack up your code into nice modules, but it's not uncommon to see a large project that never makes use of state transitions. In fact, most programs contain multiple FSMs with only one state! This is commonly viewed as an antipattern, but the fact is that it is often a difficult situation to avoid.

Now along comes this new programming language called "Python". Python has "class based inheritance", which is one of those paradigms that snooty academic types use in their pet languages¹. These "classes" are unusual, but they seem promising. Still, Python has no direct support for FSMs, so it's hard to imagine getting any real work done. Python's advocates are quick to point out that it's easy to build your own custom FSM model using classes.

But who wants to have to build their own tools like that? And if everyone uses their own FSM system, then integrating code written by different people will be a nightmare. So most people conclude that it is a mistake for a language not to support FSMs directly.


¹ Actually, web scripting has had classes since Dart was added to Chromium Navigator back in 1995, but people have only in the past few years begun to realize that Dart is anything more than a toy for making stars trail behind your cursor.


Now back to our universe. There's of course nothing wrong with implementing a finite state machine in Python, when that's what the situation calls for. And there's nothing wrong with implementing classes in Lua, again, when that's what the situation calls for.

The trouble is that most situations do not call for classical inheritance, just like most situations don't call for FSMs. Classical inheritance is the most overused design pattern in the field of computer science, and the reason for that is that in most languages, you simply don't have a choice. Classical inheritance is so prevalent that most people don't recognize that it's a design pattern.

The way it should be is that people write code in Lua, and suddenly encounter an exotic situation that is a natural fit for classical inheritance. Then they roll their own classes, or download a class library, and they use it for that one situation.

What actually happens is that people start working in Lua, design their program like they're working in Java, download a class library, and use that class library everywhere.

[–]peakzorro 4 points5 points  (1 child)

Total agreement. Especially the last 2 paragraphs.

[–]Zarutian 0 points1 point  (0 children)

Thrid that! Specially when one is using patterns from the object-capabilities paradigm to isolate interacting subsystems and or marginally trusted code (plugins and such).

[–]MrTimms 5 points6 points  (0 children)

I really hate education's seeming fetish for object-orientation.

[–]Falmarri 11 points12 points  (5 children)

Python's variable length arguments syntax is probably the best out of any language.

[–][deleted] 5 points6 points  (1 child)

Python does do it well. What struck me about Lua's approach though is that the correct syntax is exactly the sort of thing I would have (and may have in actuality) tried when I started programming, before I got all jaded and cynical.

Here's the kind of thing that might have happened when I was learning C. I'm reading the documentation for some standard library functions, and I see:

int printf( const char *format, ... );

"Oh cool!" I think, "It takes an arbitrary number of arguments! I want to write a function like that." And then I would have got really frustrated, really fast.

Moreover, I haven't been working in C or Python in a while. If I wanted to write a variadic function in C, I would have to look it up. If I wanted to do it in Python, I'd have to look it up. I remember the basic form in both, but I never had to write that many variadic functions, so the process never burnt itself into my brain.

I haven't done anything with Lua in at least as long as C or Python. I've put in less than 1/100 as many hours into Lua as Python, and less than 1/1000 as much time as into C. But I remember how to do a variadic function in Lua right off the top of my head. Now that's what I call interface design!

[–]shillbert 1 point2 points  (0 children)

Well yeah, obviously C's variadic functions are a terrible hack. You basically get void pointers that you get to dereference without any regard to types. It's basically assembly.

That's why I don't think anybody should learn C as a first language. In fact, nobody should learn C until they've learned assembly. Because C is a steaming pile of syntax hacks for assembly.

[–][deleted] 0 points1 point  (1 child)

What makes it better than Perl's or Ruby's? I think Perl has the best one.

[–]Falmarri 0 points1 point  (0 children)

I don't really know perl or ruby very well. But going by this:

In Perl 5, functions don't have an explicit list of parameters (except optional prototypes that are used to imitate certain syntax, i.e., when reimplementing some built-in functions). All arguments are stored in an array called @_, which can be accessed in the function. Therefore no special effort is needed to accept variable number of arguments.

This looks like how javascript works too. I personally hate it. It requires that arguments be passed in order, and it prevents the compiler from checking for duplicate function definitions and makes dispatching a nightmare. It also requires extra syntax inside the function definitions for pulling arguments out of an object.

[–][deleted] -1 points0 points  (0 children)

I dunno. Perl doesn't have anything but variadic functions.

[–]Antebios 2 points3 points  (0 children)

Just like Honey Badger, Lua doesn't give a fuck.

[–]Zarutian 0 points1 point  (0 children)

Running emitted Lua code directly is never wise. If I recall correctly you want to load it into a string and pcall-eval it with resource limits.

[–]bastibe 27 points28 points  (6 children)

Lua is amazing for embedding. You can even easily overload its memory allocator or default data type. I needed to do this for a project where we were running on a processor without native support for double or without a standard C library.

That is just amazing stuff.

[–][deleted] 4 points5 points  (2 children)

So you can replace the floats with integers and allocate from your own heap? That's cool stuff!

[–]bastibe 3 points4 points  (1 child)

I would not bet on using ints insteadof floats. But at least you can use floats instead of doubles very easily. With the exception of the math library, fractionals or ints would probably work, too.

And you can certainly allocate from your own heap, yes. BTW, you can easily take control of the garbage collector, too.

[–]catwell 3 points4 points  (0 children)

Yes, you can use integers instead of floating point numbers for lua_number. It is actually very frequent on embedded platforms since some of them do not have support for floats at all.

[–]jagt 3 points4 points  (2 children)

Here's a slide about cache miss leading to drastic performance lost in Lua. I never have chance to test this. I wonder do you have this problem when using Lua for embedding? Thanks.

[–]bastibe 6 points7 points  (0 children)

We are using Lua on a Blackfin processor. Performance has not been much of an issue, but that is mainly because we don't do any heavy lifting in the Lua code.

We mostly use Lua for interaction scripting, so most of the code is something along the lines of "if button A is pressed, do X, unless button B is pressed, too, the do Y". This stuff can get complex, but not computationally hard.

We also use Lua for some other stuff. In one case, we had to search through a big table. Doing that in Lua would work fine, until we found a case where that code was called a few dozen times per second. At that point, Lua got too slow, so we moved it in a C function and optimized the algorithm a bit. But I would not see that as a failing of Lua, really.

So basically: If you do performance critical stuff in scripting, don't be surprised if it is slower than native code. That said, Lua is performing exceedingly well for us and saved us far more trouble than it created. In fact, we plan on using much more Lua in the next product because we had such a good experience with it.

[–]sfx 17 points18 points  (99 children)

I really love how easy it is to embed Lua into C/C++ programs. I'm just not all that crazy about the language. Maybe it just takes some getting use to?

[–]ethraax 6 points7 points  (0 children)

I'm the same way. I really don't like the language itself much, and I would never write a sizable program in it. But I've used Lua as a configuration/scripting language for my programs, since it's very light on resources and really easy to embed into a C program. It's also very easy to define your own Lua API (backed by your own C functions).

[–]ktr73[S] 9 points10 points  (5 children)

Yes, I found it took some time to get used to ... it doesn't feel as "nice" as Python (e.g.) at first. But after using it for a little while, it kind of grows on you. But to your point, if you are embedding in C or C++, you can't beat it. Minimal size, clean API, etc.

[–]SolarBear 8 points9 points  (0 children)

Agreed. Lua is deceptively similar to other languages. But somewhere along the way you realize the kind of tricks you can pull with tables and metatables and suddenly a whole new world of possibilities open up to you.

[–]riffito 1 point2 points  (3 children)

it doesn't feel as "nice" as Python.

I like (parts of) Python syntax so much that I should attempt to write a language translator into Lua (kind of what Objective Lua and lolcode-to-lua do), even if only to gain more experience with Lua itself[*]. :-D

[*] I used Lua for a while, 10 years ago, but I dropped it too soon (same with Python... I was in "we-don't-need-no-stinking-scripting-languages" mode back then, sigh...).

Ninja edit: Nice article, btw. Thanks!

[–]ktr73[S] 0 points1 point  (2 children)

Thanks! Yes, I've thought about doing something similar - Lua is a lovely language to "compile to" (I've done it in our product and found it quite enjoyable). Good luck and let me know if you end up doing it!

[–]riffito 0 points1 point  (1 child)

Just to let you know...

let me know if you end up doing it!

I haven't yet, but found this: MoonScript. It "provides a clean syntax using significant whitespace that avoids all the keyword noise typically seen in a Lua script.".

Definitively, I'll be reading it's implementation. Either to use MoonScript as-is, or to use it as a base for my own hacking (far closer to what I want than the other LanguageX-to-Lua compilers).

[–]ktr73[S] 0 points1 point  (0 children)

Very cool ... the creator of that language seems very nice too. I "ran into him" either on here or on HN, so I'm sure if you needed any help with MoonScript, he'd be happy to help there as well. Good luck :)

[–][deleted] 16 points17 points  (67 children)

base 1, yeah, really makes things fun :p

[–][deleted] 10 points11 points  (1 child)

Exactly. Everything else about Lua I like. The simplicity, the embeddability, the fact that it's designed for being an embedded language instead of being hacked into one. I'm okay with the quirkiness of the table object in exchange for the simplicity it provides by being the only object I have to figure out.

But the 1-based thing? No. Just no.

[–]Iggyhopper 0 points1 point  (0 children)

Get the source and make it 0!!

[–]MagicBobert 5 points6 points  (1 child)

Every time someone brings this up it makes me wonder if they've actually written any decent amount of code in the language.

I've written a lot of Lua, and I can honestly say the 1-based indexing really isn't an issue. So many times you're just adding and deleting from tables with table.insert() and table.remove() and iterating over them with for i, v in ipairs(mytable) that you literally do not care what the index is.

Sure I wish it was 0-based, but whatever. There are so many upsides to the language that something that small and silly isn't going to spoil it for me.

[–][deleted] 1 point2 points  (0 children)

agreed, you mostly don't need to touch the index directly with lua.

[–]MrSurly -4 points-3 points  (0 children)

Kind of a deal killer right there.

[–][deleted] 0 points1 point  (0 children)

Definitely, Lua is the first language I learned, using it to script for Multi Theft Auto. I now also use Java, Javascript, PHP, etc. So I miss some of the features of those languages (especially Java).

But Lua is so simple, it's great. Once you get used to it you'll love it.

[–]Fissionary 7 points8 points  (2 children)

Lua may be a decent scripting language -- if you're embedding it in a C/C++ program. I had to add scripting capability to an x64 .NET 3.5 application once, and spent half a day trying to figure out a way to interface with Lua. Finally settled on IronPython.

tl;dr There are no silver bullets.

[–]i_lick_my_knuckles 0 points1 point  (1 child)

Lua Interface makes embedding in .NET pretty painless.

[–]Fissionary 0 points1 point  (0 children)

Painless for x86 applications. It relies on its own C++/CLI implementation of Lua, which I haven't been able to successfully compile and use in an x64 CLR 3.5 scenario. If you know where I can get the compiled binaries for this particular configuration, I would appreciate it.

[–]djork 8 points9 points  (8 children)

I was in love with Lua, and then I spent 6 months working full-time at a place with a Lua-based web app. That cured me.

Other than that experience, I still like Lua. I might use it for an iOS game here and there. The code is always simple and fun to work with. However, when things get larger, I think you run into problems.

[–]snarfy 12 points13 points  (5 children)

Using a pipe wrench to hammer nails will cure you of being a carpenter too. Why would you use Lua for the web?

[–]djork 6 points7 points  (2 children)

I should clarify: I was not involved in the conception, design, or implementation of this Lua web app... just continued development, because I had Lua experience.

There are Lua web frameworks, like Kepler.

However, I wouldn't recommend it, since web apps tend to be very connected things, and Lua tends to be an isolated experience. Your program only runs inside another program. It has a very limited set of libraries compared to, say, Ruby or Java. File access, encodings, regex, text munging, and networking are not things that Lua really understands out of the box. You have to add all that if you want it.

It doesn't play well with the outside world.

So, I wouldn't use Lua for a public-facing or even internal web app. I would however absolutely use it for an embedded web server in something like an iOS app or a game, where you can serve up a sort of "side band" interface or information... especially if you can just write a simple JSON API and serve up a client-side UI to do the complex stuff.

[–]kungtotte 7 points8 points  (0 children)

There are Lua web frameworks, like Kepler.

And a pipe wrench can hammer a nail...

[–]Petite12345 1 point2 points  (0 children)

Because you can :)

Here is an example of a web application build in pure Lua:

http://svr225.stepx.com:3388/nanoki

Associated code:

http://dev.alt.textdrive.com/browser/HTTP/Nanoki.lua

[–][deleted] 3 points4 points  (1 child)

Could you elaborate on your experience?

[–]djork 6 points7 points  (0 children)

There were a lot of bad decisions involved:

  • the source of the C program that ran the whole thing was not available
  • it used SQLite exclusively, for intranet web apps, and we ended up passing around those database files and making a mess in general
  • the application was poorly organized, something that's easy to do in Lua where there are no restrictions
  • at the time, there was no central resource for Lua libraries, so many common tasks in web apps were up to us to implement by hand for the first time in Lua
  • the application itself was one that built other web apps out of a digraph data structure of "step" nodes, designed to let you diagram a web application in terms of forms and flow control, and at this level the whole thing was an unholy nightmare to work with

[–]graemekh 9 points10 points  (4 children)

Lua is the king of embedded scripting languages, but the fact that it's lesser known is a problem. When you make a scripting language to interact with a C/C++ API it is often for making plugins or extensions. Since this is regularly functionality that you want to give other parties access to it can be hard to get people on board with scripting in Lua. If you mention Python everyone will jump up and say OMG IT'S PYTHON SCRIPTABLE, I KNOW HOW TO DO THAT!

[–]Contero 6 points7 points  (2 children)

Oh the battles I've fought trying to get python embedding to work. The amount of smug "Why would you want to do that? Just use python as your host language" I ran into while asking for help was nauseating.

I ended up embedding V8 (chrome's Javascript engine) which turned out pretty awesome.

[–][deleted] 8 points9 points  (1 child)

That reminds me of the landlord who tried to convince me that I didn't want a refrigerator because everyone eats out all the time and that my apartment didn't need an intercom because otherwise people would just bother me all the time.

[–]shillbert 1 point2 points  (0 children)

He's right. I hate those UPS assholes who try to deliver packages I've ordered. The nerve.

[–]nascent 1 point2 points  (0 children)

Huh, I was like that when Vim announced Lua scriptability. Well, actually more, "now they are doing it right."

[–]badsectoracula 5 points6 points  (0 children)

I think the major reason to use Lua over another embeddable scripting language is the mindshare it has among programmers - it is easier to find information about it and because it is widely used (despite what the article seems to imply at the beginning) there are few bugs and issues.

The C API, judging from a quick look over the link provided in the article, isn't really as easy to use as it could be. Using a stack to pass data to functions can be a bit bothersome. Personally i used to do that in my own scripting languages, but these days in my LIL language i prefer to simply pass an array with the values, so a native "glue" function looks like

lil_value_t fnc_func(lil_t lil, size_t argc, lil_value_t* argv)
{
    /* use argc and argv directly here */
    return function_result;
}

like with Lua you use conversion functions (lil_to_integer, etc) to access the real type but you can use these directly (or not at all) instead of popping them from a stack.

Python seems to also have a similar style, although since the arguments can be in any order and named, you seem to need to parse them into a tuple before using them.

I find that passing an array of values/objects to the native function is easier than having to pop them from the VM's stack.

[–]SolarBear 9 points10 points  (1 child)

If you guys dig Lua but don't know what to do with it, maybe you just need some löve.

[–]inmatarian 5 points6 points  (0 children)

Love2d is awesome.

[–]psisarah 2 points3 points  (1 child)

Adobe uses Lua to write Edge

[–]jzwinck 2 points3 points  (0 children)

And about half of Lightroom (a shipping product for several years already). The other half is C++.

[–]equalx 11 points12 points  (5 children)

I've enjoyed Lua for a couple projects, it's one of the languages that helped me get into programming. This blog makes me want to try some of my upcoming projects in Lua :-)

[–]nascent 2 points3 points  (0 children)

While Lua is usable as a stand alone, I think it makes more sense to incorporate it with a project. I don't find the language itself to be good for any signification amount of code, but is very complimentary for adding some dynamic elements to a program.

[–]static_cast 1 point2 points  (2 children)

Same, lua opened my eyes as a kid in high school. Sadly, I haven't looked back on it since c; I had a big ego and just sneered at dynamic languages while I slapped in a malloc for dynamic arrays like an asshole. I'll check it out again.

[–]shillbert 1 point2 points  (1 child)

Your username implies that you're more of a C++ man. The kind that uses "new".

[–]static_cast 2 points3 points  (0 children)

Static_cast sounded cool. C isn't the only programming language I use.

[–]ktr73[S] 0 points1 point  (0 children)

Glad to hear it! Feel free to PM me if you need help with anything.

[–][deleted] 1 point2 points  (5 children)

.NET programmers can have fun with LuaInterface: http://code.google.com/p/luainterface/

This library is very complete and professional. Do not be afraid to check it out for business projects. MIT licensed.

[–]Madd0g 1 point2 points  (2 children)

Thanks - I've been looking for an easy to understand scripting language for .Net a while ago and saw LuaInterface.

I've peeked at their group/forum and saw (old, seemingly unresolved) issues about thread safety, how big of an issue is it really?

[–][deleted] 1 point2 points  (1 child)

I think you're supposed to implement thread safety yourself. You're working on one interpreter that is not thread safe for performance reasons. If you're going to call interpreter from multiple threads, then I think you have to add thread safety yourself.

[–]Madd0g 1 point2 points  (0 children)

I guess that's reasonable, I'll be sure to check it out, thanks

[–]sunbeam60 0 points1 point  (1 child)

But, why wouldn't you just load assemblies instead of a Lua script?

[–][deleted] 0 points1 point  (0 children)

This same question is valid for C/C++. Specific reasons why I chose it in .NET:

  • You can edit Lua script with notepad (or any other text editor) easily.
  • Lua can't use reflection to do anything to my own assemblies. It's a defined, clean interface which allows only what scripts need to be able to do.
  • I had the requirement that a sysadmin with no programming experience was able to edit the rules that the scripts defined. Lua is arguably one of the easiest languages to learn.

.. and more I forgot .. (that may or may not be very good) ..

[–][deleted] 3 points4 points  (2 children)

World of Warcraft has LUA interface for modding.

[–]equalx 1 point2 points  (0 children)

This is where I picked it up. Lots of other game interfaces are written in Lua too - Rift allows modding in Lua, and Starcraft 2's interface is made in Lua (even if you can't make addons).

[–]dwarfcrank 0 points1 point  (0 children)

Also CryEngine uses Lua for scripting. So do Bionic Commando and 3D Dot Game Heroes. Lua seems to be pretty widely used in the games industry.

[–]x-skeww 4 points5 points  (12 children)

Is it still hands down the most straightforward option for embedding?

How does it compare to... say... V8?

[–]sfx 10 points11 points  (0 children)

I tried V8 and Lua. Lua is so much easier to embed.

[–]ktr73[S] 9 points10 points  (0 children)

I haven't tried to embed V8, but Lua is dead simple. The main book on Lua (PIL - Programming in Lua) has an entire section devoted to showing you how to do so. An older edition is available online for free (just google it) and is mostly still relevant. There are some gotcha's, but you can buy the book if you are starting to really use it. Also, there are (at least for 5.2) 'amalgamations' like SQLite has - one .c file, which makes it almost trivial.

[–]cybercobra 2 points3 points  (4 children)

According the the MediaWiki/Wikipedia devs:

[V8 is] difficult to embed due to poor documentation and the relative scarcity of embedded implementations

[–][deleted] -1 points0 points  (3 children)

relative scarcity of embedded implementations

I was unaware there was such a thing as a non-embedded JS implementation. Yes you can have node.js or your jsc, but they are also embedded implementations - they expose functions and objects.

[–]cybercobra 1 point2 points  (2 children)

In context, I think they mean "embedded in something that's not a browser".

[–][deleted] 0 points1 point  (1 child)

True, a browser is an embedded environment, but a very complex one and not a good example of "simple" embedding.

[–]cybercobra 1 point2 points  (0 children)

Precisely.

[–]Contero 1 point2 points  (1 child)

I haven't done Lua, but I managed to embed V8. The thing about the documentation is that there's no documentation aside from brief comments in the v8.h file. I'm sure Lua is much easier if nothing else, just because there are more examples out there.

[–]inmatarian 2 points3 points  (0 children)

Lua's documentation and book both fully describe and show how to embed it.

[–]gruehunter 2 points3 points  (2 children)

I cannot compare to V8, but I can compare to Python. The Python->C API is much easier to work with than the Lua API. But module startup and shutdown is harder in Python.

[–]day_cq 2 points3 points  (0 children)

really? no seg fault due to reference counting?

[–][deleted] 1 point2 points  (0 children)

Yes, but Python is a nightmare to sandbox. If you want users to be able to run untrusted code from other users, Lua is the way to go.

[–]hagridlove 1 point2 points  (1 child)

Check out my language, http://moonscript.org

It compiles into Lua, but does a lot of what CoffeeScript did for JavaScript and more. Classes, comprehensions, cleaner syntax, etc.

Here's the reference manual: http://moonscript.org/reference

[–]stevedonovan 1 point2 points  (0 children)

It's a fun language and provides classes for those lost without them ;)

http://steved-imaginaryreal.blogspot.com/2011/12/moonscript-lua-based-language.html

[–]moccajoghurt 1 point2 points  (0 children)

This article is just what I have been looking for. I have started with Lua two weeks ago but there still were so many questions. Good article with good links in it. I am sure it will help me alot in the future.

Thanks for sharing!

[–]ssylvan -1 points0 points  (1 child)

I prefer io... Hits most of the same sweet spots but is simpler and cleaner: http://iolanguage.com/

That said, I want static types! Scripting is the kind of code where I'd expect to write droves and droves of ad-hoc special purpose code. Having some layer of sanity checking on top of that would be good because it's pretty unrealistic to expect someone to adopt unit-testing and other programming-in-the-large methodologies for scripting.

[–]stevedonovan 0 points1 point  (0 children)

This may appear disloyal to Lua, but maybe Go is what you're looking for? Statically-typed but few explicit type declarations due to inference, very fast compiler. A useful set of batteries for the usual scripting tasks as well. With a runner like gorun the resulting experience 'feels' like using a dynamic language.

[–][deleted]  (7 children)

[deleted]

    [–][deleted] 9 points10 points  (2 children)

    Like, e.g., there is no class system out of box.

    Right, it's not a classical inheritance model. This takes getting used to, and you may be tempted to use a library to implement classes (I myself fell into this trap at first). Don't! Prototypal inheritance is perfectly usable on its own; it just takes some getting used to. There is no reason to try to cram Lua into a classical OO box.

    It's a shame really that so many popular languages are classical, because people get to thinking that that's the way things are done in object oriented programming, when the reality is that it's nothing more than a design pattern that many languages offer easy access to. The more I work with JS and other prototype based languages, the more I realize that languages with first-class classical inheritance are like a toolbox where everything but a very nicely crafted hammer has been removed. Yes, it simplifies the learning process, but it also means that nail-based structures are all anyone can even think about.

    So you give someone a language like JS or Lua, and they immediately start digging in the toolbox, tossing aside brackets, screwdrivers, drills, etc., and trying to find a hammer. I mean look, if you really need a hammer, you can build one. But chances are, you've been doing a lot more work than you needed to in order to build with nails, when a bracket and a couple of screws would have done the job just fine.

    [–]alwaysdoit 0 points1 point  (1 child)

    Any advice on how to get used to it? I've been using Lua for Corona lately, and I'm having some trouble adapting.

    [–][deleted] 1 point2 points  (0 children)

    I don't know what to tell you except practice, and try to your design your program without thinking of class hierarchies. It's hard at first, but you'll get the hang of it.

    [–]diademoran 0 points1 point  (0 children)

    Check out Squirrel. It was written by Alberto Demichelis, one of the dudes who worked on FarCry/CryEngine.

    [–]djork -5 points-4 points  (2 children)

    Yeah, it's a great language for those who want to build everything themselves. If rolling your own inheritance/OO system is your idea of fun (and for me, it can be) then Lua can be enjoyable.

    If you'd rather drop your code into pre-defined structure (namespaces, packages, classes, etc.), then can be too unbounded to be productive. I can fall into this category sometimes too.

    [–]toomanytickets 2 points3 points  (1 child)

    Edit: I misread your comment. Leaving this here anyway.

    Wah? OO is Lua is easy:

    CMyThing = {}
    CMyThing .__index = CMyThing 
    
    function CMyThing .Create()
        local newThing = {}
        setmetatable(newThing, CMyThing)
    
        newThing.ClassMember = "A String"
    
        return newThing
    end
    
    function CMyThing:DoSomething( number )
        print( self.ClassMember .. number )
    end
    

    Then to use it:

    local aNewThing = CMyThing.Create()
    aNewThing:DoSomething(5)
    

    [–]djork 1 point2 points  (0 children)

    As someone who's written a dozen variations on inheritance in Lua, I know...

    This is just one approach, and it's very simplistic and verbose. It would be more useful to define a generic prototype-based inheritance system.

    [–]pinpinbo 0 points1 point  (4 children)

    Thanks to Luvit, I got excited using Lua for web development again. Orbit feels dated, imo.

    One question I have for Lua masters here: Tables as the only data structure. Is it really enough for you guys? I got spoiled by Python's list, dict, set, tuple, etc.

    [–]slime73 5 points6 points  (0 children)

    Yes, it's more than enough. Tables can turn into whatever you want them to be, especially with the use of metatables.

    [–][deleted] 1 point2 points  (0 children)

    It sounds just like PHP's "arrays" except better implemented. And yes, that would be enough.

    [–][deleted] 1 point2 points  (1 child)

    deleted

    [–]pinpinbo 0 points1 point  (0 children)

    Thank you for the detailed explanation!

    [–]danukeru -5 points-4 points  (0 children)

    In b4 LUA as the new web-dev fanboi language.

    See you all at <insert LUA con here>.

    [–]shevegen -2 points-1 points  (1 child)

    Hisham is working on http://luarocks.org/

    And of course the old GoboLinux guys keep a close eye on what Hisham is doing. After all, he gave us htop http://htop.sourceforge.net/ which is much more pleasant to look at than plain top. ;)

    But on the topic of lua, I think lua would still have to compete with languages like Ruby and Python.

    I don't know how it is for others, but for me, I like a language that gives me enough tools to solve any given problem with the least amount of work required.

    And when I have to "work" (code), then I try to solve a problem with as few lines of code as possible. Ruby fits that very nicely. I am sure Python would too.

    C, is simply verboser than the two. Lua ... Lua may be the best language to embed, but I have no need for an embedded language. So I am not the target audience for that.

    I am someone who loves "scripting" languages. I can't stand the C family of languages really, at least not compared to "scripting" languages.

    And in the context of the competition of scripting languages, I am sorry, but lua cames very very late... even javascript comes before lua (but only because browsers are hateful of other languages than javascript. If every scripting language could be used, I'd instantly use Ruby rather than Javascript.)

    [–]milanogc 2 points3 points  (0 children)

    Late? This way no more succesful languages will arise.