9 ms·
Lua, a Misunderstood Language
- 1vuio0pswjnm7 6y agoPeople employed as programmers who are vocal in mailing lists, forums, blogs, etc. can have some very warped views on programming. Curiously, their statements often take a didactic tone. It seems they want to tell others what to do and what not to do. Sadly, these are not the folks who anyone should be attempting to "learn" from. They are antithetical to curiousity. Nice to hear something from someone who is not working as a programmer. This is a much clearer perspective, IMO. At the very least, regardless of anyone's opinion, it gives us a more balanced perspective.
- fwsgonzo 6y agoI have always understood the reason why Lua keeps getting embedded into games and game engines is because it's a language that you can reasonably hope non-technical people to be able to contribute a little bit from. I know that from experience myself. I've moved on from Lua to using tiny virtual machines that run RISC-V instead, and so I choose to use Nim inside the VMs in the hopes that non-technical people will find that easy to grok, somehow. I don't have any anecdata to report yet because it's still early in the project. But, that's the only thing I consider when choosing the language.
- TulliusCicero 6y agoI've had the opposite experience. Teaching my 9 year old son Lua has been kind of painful, in large part because of language decisions that make learning things harder for beginners. For example, did you know that if you pass in a variable to a function, when that variable doesn't actually exist, Lua won't complain? I don't just mean that there's no compiler, but even the runtime is fine with it. It'll just silently assign the argument value to be nil. This resulted in my son being very confused, after having a typo in the variable name he was using there. Why is the value nil? He was sure he gave it a value earlier, before passing it into the function (and he was correct, to an extent). I have a laundry list of complaints about the language, things that make it harder to teach beginners. Sure, there's less 'setup' than, say, Java, and that's nice. And the 1-indexing is fine, really. But there's other language decisions that are just weird, man. Why would you make this 'table' structure so flexible and important, and then completely neglect to provide a built-in function create copies of said table? How is that non-programmer-friendly? How is providing no ++ or += increment operators better for beginners?
- striking 6y agoI gave you an upvote because I think you don't deserve to go negative just for raising some of these concerns. Still, I'm going to examine some of them. With regards to variables, the best way to hack around this issue is to 1) always use `local` when declaring a variable and 2) set `_G`'s metatable to raise an error if you try to create a new key, which prevents accidentally using globals like you've inadvertently discovered is the default behavior. This is this way because it is I guess simpler than teaching someone how declarations work, but it obviously leads to a pretty immediately bad situation for anyone who wants to do more programming than setting a global constant. It harkens back from when Lua was basically a TCL-like. Copying a table is generally unnecessary, if you never mutate the table to be cloned. Just set the __index metatable property of a new table to a reference to the table to be cloned. Ta-da, you have inherited the previous table. Not giving you an immediate, easy way to do this forces you to come to terms with copying not always being the right solution. It's not great, and the language offers no pointers to those who want to get a thing done, but it isn't the worst choice. Not providing increment operators is a design choice, one that simplifies the metatabling for operators. Only having one case for addition, and not having to worry about increment / incremental assignment, means people creating metatables that override operators have a good experience. This is one of those times where power users are given the advantage at the cost of the less experienced, and I share your implied concern in feeling the inconsistencies in who is favored by which language features.
- TulliusCicero 6y ago> With regards to variables, the best way to hack around this issue is to 1) always use `local` when declaring a variable and 2) set the `_G` metatable to never allow the creation of keys Keep in mind, I'm talking about beginners here. Beginner kids, even (it's a popular language for Roblox scripting!). Are either of those things, things that will just come naturally to newbies? Are they definitely gonna know about them when they pick up the language in random text or video tutorials? Are they going to remember to use local as often as they can, and will they understand when it's appropriate to use a global? The answer to all of these is no, obviously. For someone like me with a CS degree and several years of professional experience, your suggestions are perfectly reasonable, but in the wide, wacky world of beginners hacking things together, the right way should be easy and obvious. So obvious, even a beginner will catch on. > Copying a table is generally unnecessary, if you never mutate the table to be cloned. Just set the __index property of a new table to a reference to the table to be cloned. Ta-da, you have inherited the previous table. How is this more intuitive than just having a copy or clone function? You can find plenty of cloning functions on stackoverflow or wherever that people have written. Yes, there are workarounds, but why do you need a workaround for a pretty basic use case here? In python, you can just go "dict2 = dict(dict1)", or "dict2 = dict1.copy()", both of those are easy enough, and more importantly, they're intuitive: want a copy? Call the copy function! Why does Lua have to make it weird? > This is one of those times where power users are given the advantage at the cost of the less experienced, and I share your implied concern in feeling the inconsistencies in who is favored by which language features. Yes, it seems like the language design is a strange mix of "really easy to get started for beginners" and "footguns that you probably need to be experienced to avoid".
- virtue3 6y agoAs someone that worked on LuaJIT for embeded devices (IE phones), I have some strong opinions / context. Lua is designed to be small and portable and highly embeddable. It's not the greatest language in the world, but understanding it's interpreter and how to use it from your system side is pretty easy as these things go. It's really REALLY well suited to be embedded into whatever system you need it in. The whole runtime is like nothing. And it runs damn fast for an embedded language, there's like 10 opcodes or something? compared to pythons 100+ (at the time we did the comparison like 10 years ago). Further context, I think we ported our android luaJIT to windows phone 7 in like, a day? It was super trivial.
- monocasa 6y agoI count around 100 opcodes for luajit as well. http://wiki.luajit.org/Bytecode-2.0 http://wiki.luajit.org/Bytecode-2.0 Not sure why reducing the number of ops by an under of magnitude would make it go faster.
- lrossi 6y agoEmpirically, it is much faster than Python, but I don’t know either if the opcodes have anything to do with it.
- monocasa 6y agoRight, but reducing the number of ops wouldn't get you there, that just adds more branches in the critical path as the same amount of functionality needs to be overloaded.
- mobilio 6y agoLua it's very popular on routers because it's using on some device vendors like Linksys and some opensource distributions like OpenWRT
- austinl 6y agoOddly enough, Lua was my first introduction to programming. I was into Valve's Half Life games and Garry's Mod, and Lua was typically the way that you'd make modifications to things (e.g. increase player movement speed). That would interact with the Source engine, which was in C++, but you could do a lot of stuff without ever touching C++ [1]. I didn't even really think of it as programming at the time. You start by just changing constants, like what if we make gravity = 2 instead of 9.8 m/s2? And then build up from there. I think is what a lot of these educational programming games try to replicate today. After a while, I got confident enough to the point of trying to do things in LÖVE, which is a free and open-source 2D game engine that uses Lua [2]. What I learned there is the reason why I eventually studied computer science in college. That said, when I return to Lua, it feels a bit limited, but I suppose that's the point! I never really questioned it at the time. "Lua is not object-oriented. That is it." Laughed out loud when I read this. It was funny to learn programming without the concept of classes, and then realize that Lua is the exception, not the rule. [1] https://developer.valvesoftware.com/wiki/Embedding_Lua_in_the_Source_Engine https://developer.valvesoftware.com/wiki/Embedding_Lua_in_th... [2] https://love2d.org/ https://love2d.org/
- andrewmcwatters 6y agoThanks for linking my article. I went on to go build Half-Life 2: Sandbox as an open-source competitor to Garry's Mod, and later Planimeter's Grid Engine, which is also based on LÖVE.[1] [1]: https://www.planimeter.org/grid-sdk/ https://www.planimeter.org/grid-sdk/
- deergomoo 6y agoLua was mine too, also via games. I’d installed custom firmware on my PSP and after getting my fill of emulators and rampant piracy, I wanted to see if I could make a game. Getting the C-based toolchain working was far beyond 12yo me at the time, but there was a “Lua Player” homebrew application that would load scripts off the memory stick. I didn’t get far with it and to be honest I remember very little, but it did set me off down the rabbit hole of learning to program, and now it’s both my career and a big passion.
- leke 6y ago
- d--b 6y agoAs a dev who’s only tried Lua for a small dev project, I have to agree with the no-batteries argument. It is so bare that i ended up implementing tons of function that i wished were there in the first place. Not asking for lua to become the new javascript but a library like “lodash for lua” would be appreciated...
- epage 6y ago> Complaints about 1-based indexing for arrays I think this misses the point, at least for my case. It doesn't matter which is technically better. What makes more sense for humans is a matter of application (and even "non-dev" stuff varies as to which is better). For me, my big concern about using Lua in my products is - I expect other developers to be writing the Lua scripts - I expect the developers to be frequently using 0-indexed languages In this scenario, "being different" means you are likely to run into off-by-one errors. Think of that crufty Perl script you might have around at work where people avoid touching it because no one is comfortable enough with Perl. Having predictable semantics helps a lot in scenarios like this.
- noizejoy 6y agoIt doesn’t miss the point, because the point was that non-developer humans will do better with 1 based indexing. Side note: As a former developer, turned regular human being who scripts from time to time, I don’t find switching between 0 based and 1 based indexing particularly intellectually taxing.
- TulliusCicero 6y agoYeah I have a bunch of complaints about the language that I developed while learning it and simultaneously teaching my 9 year old kid, but the 1-based indexing isn't one of them. That's totally fine, and certainly easier to grasp for a beginner. But there's certainly other stuff that's just weird and bad, and I think just the fact that it's dynamically typed made it harder for him to build an accurate mental model -- sometimes he didn't even realize that an identifier that he had typed in earlier was a variable, let alone what kind of value it'd be holding.
- zajio1am 6y agoWell, i started programming in Pascal (with 1-based indexing) and later switched to C. I remember that after switching to C off-by-one errors magically disappeared.
- billfruit 6y agoWait till you need to use a formula which has somewhat complex uses of indexes like 2^(2n+1) and see how having deal with them in 1-base indexing in addition to 0 based indexing makes it add bit of unnecessary confusion.
- TulliusCicero 6y agoSounds like the author is talking about me. I'm a programmer who recently started teaching his 9 year old son Lua, so that he can use the Undertale fan game platform Create Your Frisk. It's gone okay, but there are things about the language that just seem weird and annoying and counterproductive. For example, tables are very important in Lua as a sort of all-purpose data structure, that's cool and all, but why is there no built-in copy function? The author of this article seems to be hinting that this anti-batteries-included style grants some sort of freedom to do it myself, which is just kind of silly. I don't want to do it myself -- and there's a good chance I'd cock it up if I tried -- my son certainly isn't going to do it himself, and even if they had provided such a function, that doesn't actually stop anyone from doing it themselves anyway, if they want to. Or, why are there not only no increment operators like ++, but even no operators like +=? "counter = counter + 1" isn't only longer to type, it's harder to quickly parse. Why is the standard style for multiword identifiers neither camelCase nor underscore_case (or whatever it's called), but instead just runthemalltogetherit'stotallyfine? Why is the default for the random number generator using the same seed every time you run the program, and why does os.time() provide time not in nanoseconds, or milliseconds, but solely in whole seconds? Why doesn't the runtime complain if I pass in a completely non-existent variable as an argument into a function? While the teaching has gone fine overall, the language actually works against our purposes by making it harder for my son to build a mental model of what's happening. The last point in the previous paragraph, for example: it's much more confusing for a newbie coder to have to deal with a 'sudden' nil value that they weren't expecting, than to have a compiler or runtime just crash and say "hey, I don't recognize this variable at this line number". And yes, this isn't hypothetical, this actually happened while I was teaching him. Same thing for the dynamic typing, and default global status of variables. The lack of 'declaring' variable types, or even that a thing is a variable at all, has meant that it's been more confusing for him to identify what kind of value a variable is holding, or even that an identifier is actually a variable at all. That variables are by default global has made it harder to teach him about passing in arguments -- he'll happily pass in some variable as an argument, and then use that variable name within the function, rather than the argument name defined in the function signature. These are the kinds of beginner-level problems that you forget about as an experienced programmer, but when you teach a kid, suddenly become very real. As I was using the language and teaching with it, I just couldn't help but feel that more modern statically typed languages like Swift or Kotlin would be flatly superior for the task of instructing a young mind on the the most fundamental task of beginner programming: getting them to build an accurate mental model of "how this actually works, how does everything relate to each other". You would get the enforced structure of older static languages, but with more modern syntax and ease of use. To me, it's hard to see what Lua would offer as an advantage, really. But then again, I'm an Android developer, so maybe it's just my own experience biasing me.
- lxe 6y agoLua static binary can be made to be less than ~150k, while providing very advanced high level dynamic language features you'd expect from JavaScript or PHP. I used it to build a admin UI for an embedded device without an MMU. The only other alternative available was C/C++, which slowed the iteration cycle significantly. Yeah it does things in a strange way, but when I used it, those quirks are vastly overshadowed by its benefits.
- jcims 6y agoDid you compile to bytecode on or off the device? I’m thinking about trying to add Lua support to a small motor controller to provide some more advanced capability inside the control loop. But I don’t actually have any experience doing this, and I can’t even decide if I want to try to compile the bytecode on the device or do that off the device and push the blob to the device. It’s pretty capable microcontroller, STM32G4.
- contingencies 6y agoIf anyone wants to be paid for porting https://github.com/nodemcu/nodemcu-firmware https://github.com/nodemcu/nodemcu-firmware firmware to new ARM hardware + ethernet (not wifi) let me know. Other ongoing embedded work available.
- lxe 6y agoOff device cross-compilation of the Lua interpreter binary, but no bytecode compilation -- it was interpreted Lua from what I can recall (10 years ago). Can't tell what's possible now, and I feel like a microcontroller might be too much for Lua.
- 1vuio0pswjnm7 6y agonmap's "nse" scripts are perhaps an interesting example of lua's utility https://nmap.org/nsedoc/scripts/ https://nmap.org/nsedoc/scripts/ and if by chance you have nmap installed: /usr/share/nmap/nselib/ powerdns also make heavy use of lua, both for configuration and manipulating traffic
- aeturnum 6y agoMy first real job after school was working on Lua (5.1a mostly) in an embedded context. It was much faster than iterating on the QT C++ application that the Lua was embedded in. It would, eventually, even allow us to do custom targeted push updates to our software (in 2009). So I love Lua. I appreciate its speed and its flexibility. I think of it as Javascript's easy to use cousin. Straightforward to write and debug - you control the entire environment. I also think this idea is deeply silly: >People who want batteries want a ready-made powertool full of opinions. They are not looking towards Lua in the context that Lua wants to exist. I would have loved ready-made powertools! I wouldn't include all the tools in my toolkit (and we already added some by building our own Lua with a few patches), but it sure would have saved me time writing them (and writing them badly as I was just out of school). I don't think a complicated, robust library would hurt Lua's mission and I'm sad to see this attitude in the community. I understand that Lua may be too small to support a "big" set of official libraries, but I think the idea that offering fewer options is better than more options is wrong.
- soapdog 6y agoBut there are ready-made powertools. There are even really nice "standard"-like libraries such as penlight. Depending on the environment you're using, you can have a Lua distro that already contains all the most useful libraries already baked in. That stuff is out there already for those who want it. In my opinion, one should assemble their own. You don't need to develop everything yourself, but you can grab the libraries you know you'll need/want and craft a little baseline Lua for you. LuaRocks can do that with some clever argument passing and fiddling with the search paths. Then, you're in control again, and your powertool is definitely yours.
- aeturnum 6y agoOh Penlight looks great! It seems like it's six years old, making it 4 years in the future for me in my first job. I was more talking about how frustrating I find the "less is better" attitude than complaining about the specific Lua libraries available today.
- deleted 6y ago[deleted]
- noizejoy 6y agoI’ve discovered Lua only very recently as it has a good presence in some music making applications for scripting e.g. for the Steinberg HALion sampler/synth and for Native Instruments Creator Tools. And more recently I bumped into the interesting VST/AU plugin called protoplug[1], which makes it easy to experiment with coding audio and midi event processing inside a DAW. [1] https://www.osar.fr/protoplug/ https://www.osar.fr/protoplug/
- PaulDavisThe1st 6y agoSee also: https://manual.ardour.org/lua-scripting/ https://manual.ardour.org/lua-scripting/
- fogleman 6y agoDijkstra's well-known argument for zero-indexing. (Hint: it's not about pointers in C) https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...
- Jtsummers 6y agoThat came up a few days ago, but in the context of Julia. I wrote a long response at the time [0]. I find it interesting that that's his title ("Why numbering should start at zero") but it really only makes sense as an argument (that starting at 0 is best) if you accept that this is the proper notation for ranges: a <= x < b If you accept that, then the rest of his argument follows. But if you use: a <= x <= b Then 1-based (where a = 1) indexing actually makes a lot of sense as well, because the other reasons for selecting his preferred range notation fall away. 1-based is only "weird" if you use his range notation because then you describe ranges as: 1 <= x < N+1 Where N is the length of the range. But, if you use the second form: 1 <= x <= N It reads fairly well. [0] https://news.ycombinator.com/item?id=25723676 https://news.ycombinator.com/item?id=25723676
- samatman 6y agoDjikstra's argument is, pound for pound, one of the densest things I have ever read. If you spend some more time with it, and you really should, you will discover that most of it is in fact arguing that what he calls convention a) is the best choice. So what you're saying here is circular; of course if you accept his argument, then you accept his conclusion, which is the title of the paper! You really should accept his argument though. It's quite persuasive. You're really just saying that 1-based indexing is okay if you use convention c), as indeed, Lua does. But you're not even touching on why he says not to. If you do, I'm confident you'll conclude, as the field in general has, that a) is the more powerful and general choice. But I would like to do more than just gesture at Djikstra's magnificent argument and say "reread this!". The One True Wiki has a useful discussion of the subject: in particular, I find the observation that 0-based indexing unifies counting and measurement to be persuasive, and it's found nowhere in the paper in question. https://wiki.c2.com/?ZeroAndOneBasedIndexes https://wiki.c2.com/?ZeroAndOneBasedIndexes
- diego_moita 6y ago> Lua is small enough that newbie developers can understand its internals well and keep it in their mind. This is, by far, Lua's best "feature". If you know programming you can learn 90% of Lua in 15 minutes (metatables and the C stack interface takes more time). For a language meant to be used by non-programmers this is more than precious, it is essential. Edit: my favourite Lua-enabled tools: 1) Hammerspoon, an automation tool for OS-X (like Windows' AutoHotkey) https://www.hammerspoon.org/ https://www.hammerspoon.org/ 2) Openresty, Nginx turbo-charged in Lua: https://openresty.org/en/ https://openresty.org/en/ 3) Wireshark, a powerful network traffic analyzer: https://www.wireshark.org/ https://www.wireshark.org/ Many others at: https://github.com/LewisJEllis/awesome-lua https://github.com/LewisJEllis/awesome-lua PS: on Windows I highly recommend the ZeroBrane Studio IDE. It is really fantastic.
- pwinwood 6y ago> PS: on Windows I highly recommend the ZeroBrane Studio IDE. It is really fantastic. Recommended by me too. The ZeroBrane Studio IDE is cross platform running well on macOS and Linux. Disclosure: I am the original author of wxLua which ZeroBrane Studio is built on.
- dang 6y agoThe previous related thread is https://news.ycombinator.com/item?id=25794374 https://news.ycombinator.com/item?id=25794374.
- ZoomZoomZoom 6y ago>You can grab the source for the version you want, and then never again look at Lua community. Sure, but it's probably not the best move if you're planning on any collaboration or second-party input. Ambitious projects require more brainpower than one average programmer has. And that's not even considering multiplication of resources spent on similar things. The gist of that article from LWN is that a lot of people see Lua as the perfect base for building "flying boats", but what lacks is some semi-official directions for the part of the community which is interested in such endeavors. In my opinion this wouldn't harm the language in any way.
- soapdog 6y agoWhat I am arguing on the post is that there are many "flying boats" built with Lua already. There are batteries-included distros of Lua. They don't need to be official, and in my opinion they shouldn't be official.
- surfingdino 6y agoWe should forget about Lua. We have better languages today. I did write code in Lua quite recently and was banging my head against lack of bitwise operators, which were added in the version later that the one that ships with the package I was extending. It is a language written in what seems to me to be a very organic manner with features added as and when they were needed, not per design, with predictable results of basic features that programmers today expect missing.
- jwcacces 6y ago>When a baby completes its first roundtrip around the Sun, they’re one year old, not zero years old. When the baby is born it is, in fact, zero years old...
- wnoise 6y agoYeah, I was going to say that this makes the exact opposite point that he thinks it does.
- type0 6y agoIt has a place, another one that's also often misunderstood is awk
- soapdog 6y agohey, I'm the author of the article. Thanks for linking. That being said, I'm always impressed with the fervour and effort that people put into complaining about Lua 1-based indexing. Folks, using a Meta Table, you can change the indexing to be 0-based. It is such a small thing. Dissing a language full of potential and flexibility because of that is overreacting. Also, those wishing Lua was friendlier environment catering to the needs of young audiences should remember that Lua was not created for that market, but that an environment that does that can be built on top of Lua. You want teach your kid a fun language, teach them a Lisp. It is easy, regular, and fun. Go with Racket. The error messages are good. Or, help someone who is making a Lua-powered game development environment make their app more friendly towards younger audiences. That is not a language's fault. The main part of that article is to approach Lua as a toolkit. Do you want zero-based arrays? Do you want errors to be triggered for undeclared variable access? You can built a Lua environment with all of that. Want batteries, you can add them too. For those asking about table copy, lodash, etc. Lua is minimalistic. If you want to add a bit of weight, I recomend using penlight library: https://lunarmodules.github.io/Penlight/ https://lunarmodules.github.io/Penlight/ (for example, here is table copy function from penlight: https://lunarmodules.github.io/Penlight/libraries/pl.tablex.html#Copying https://lunarmodules.github.io/Penlight/libraries/pl.tablex....)
- TillE 6y ago> those that are on the "Lua should hold my kid's hand while programming" bandwagon This is such an odd, needlessly hostile way to address people. What "bandwagon" is this?
- 6y ago
- bawolff 6y agoPersonally i get much more upset over lack of ++ operator than 1-array indexing. But either way its such a minor complaint.
- soapdog 6y agoI don't know if I am misremembering it but I believe that in a talk called "How much does it costs?", Roberto (one of the Lua creators) touch a bit on why there is no ++ and --: https://www.youtube.com/watch?v=EUvgoxBm7uc https://www.youtube.com/watch?v=EUvgoxBm7uc
- SergeAx 6y agoRedis has Lua engine inside since 2.6, allows lots of fun things. One can implement a distributed set of finite state machines on a Redis server, and get a key-level consistensy for free due to Redis' single thread nature
- haunter 6y agoI learned Lua with the WoW addon book in a summer https://www.amazon.com/Beginning-Lua-World-Warcraft-Add-ons-ebook/dp/B004VHODZQ https://www.amazon.com/Beginning-Lua-World-Warcraft-Add-ons-... Honestly it was an awesome experience both playing the game, seeing the game from a different pov, doing meaningful mods, and last but not least learning Lua. Still one of my favorite book even though it's outdated to some extent cause WoW and the whole backend changed so much. I wish there was something similar today as well for the "new generation".
- hisham_hm 6y agoI've seen a few comments mentioning the lack of static type checking, so I think this is probably a good time to mention my new(ish) project Teal: https://github.com/teal-language/tl https://github.com/teal-language/tl It's a statically typed dialect of Lua, which is compatible with all modern Lua versions. The core compiler itself is one tl.lua file which can be added to any Lua project, and includes a Lua package loader that makes Lua's `require()` support .tl files, and there's also a tl CLI for generating .lua from .tl files.
- selfishgene 6y agoLua would have been a nice to embed as the scripting language for Autohotkey.
- junon 6y agoI was writing a game in Lua using LOVE. I used to really enjoy and appreciate Lua. I've even integrated it into some applications using the C libraries in the past. It's always served me well. But during the development of the game, things got so unruly that we ended up having to abandon it. The code itself was written as good as it could possibly be given the engine and for Lua, but we had a lot of really poor-performance hacks for the 1-based indexing, for example. Further, the lack of proper operator overloading prevented us from using certain libraries to bind to it, e.g. Kiwi, for UI layouts. Not sure I'll ever use Lua as a scripting host in the future. There are a few alternatives I really think could suit the job better.
- unanswered 6y agoThe birthday example is evidence against 1-based indexes, not for them: on the birthday when you're turning "1", it's your second visit to that date. Your first visit to the date of your birth, you're "0".