22 ms·
Lua: Good, Bad, and Ugly Parts (2012)
- chirsz 5y agoI hope something like `ArrayBuffer` and `TypedArray` would be added to Lua, like to JavaScript.
- anonymoushn 5y agoFor better or worse, the language devs have a goal of keeping things simple, so this would have to be provided by a library. It's pretty easy to implement this sort of thing in luajit or provide that library to other luas though.
- edflsafoiewq 5y agoYou can use LuaJIT's FFI. local ffi = require 'ffi' local arr = ffi.new('float[?]', 100) LuaJIT's FFI is pretty great. It puts even a lot of compiled languages to shame.
- Normille 5y ago
- dang 5y ago"Please don't complain about tangential annoyances—things like article or website formats, name collisions, or back-button breakage. They're too common to be interesting." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- linkdd 5y agoLua is a very nice piece of technology. Its source code is pretty easy to get into, the documentation is complete. It has its quirks yes, but if I need to add scripting to a software, I'd consider Lua before considering writing a DSL, simply because you can pretty much embed Lua's source in your C/C++ software as a static library[0]. The stack-based approach makes it so easy to interact with C/C++, and I've been looking at the Rust bindings[1] recently out of curiosity, looks promising. [0] - https://github.com/lubgr/lua-cmake [1] - https://github.com/amethyst/rlua/blob/master/examples/guided_tour.rs
- VWWHFSfQ 5y agoI believe mlua [0] is the recommended Lua Rust binding now. [0] https://github.com/khvzak/mlua https://github.com/khvzak/mlua
- linkdd 5y agoThanks, I'll take a look
- cies 5y agoIf you need a few line here an there scripting, sure Lua, JS, PHP... whatever works. Using these languages for large projects is where the trouble starts: as they push you towards bad solutions that we know are bad for many decades. You need clear description of how to avoid these pittfals, or your growing codebase slowly becomes unmanageable. I ended up ditching a growing Lua codebase for this reason. Get out before it gets to big to get out.
- zerocount 5y agoI'm not convinced scripting languages are not suitable for large projects. It's said a lot, but I've never seen proof one way or the other. I suspect unmanageable code-bases derive from uncontrollable project forces, ignorance, or not caring, but not because of the perceived short comings of a scripting language.
- DoctorDabadedoo 5y agoIt's a trade off, I think. A compiled static language forces you to behave a certain way at the expense of flexibility/expressiveness, script languages are more flexible, but in a way that is easier to acquire technical debt that might be harder to move away from (IMO).
- agumonkey 5y agoI'm sure with time, the dynlang prototype phase -> static in production will be the norm. Gradual typing has been improving (might not be the only way but still)
- nerdponx 5y agoI really appreciate that the article has a "Different" section for things that are different from, but not necessarily better or worse than, other programming languages. This is also a very good summary, and tracks with my own experience getting into Lua for Neovim scripting.
- spc476 5y agoThis was written just after Lua 5.2 was released. There have been improvements since then. Lua 5.3 introduced an integer type (64-bit), actual Boolean operators, a UTF-8 library, a way to yield across a C-boundary and a way to pack and unpack binary data. Lua 5.4 introduced deterministic finalizers (and a fall out from that is a limited form of constant local variables).
- rurban 5y agoBut those improvements backfired, as Mike Pall refused to update luajit with them. They have now a 10x slower language implementation.
- strenholme 5y agoIf you want Lua which is luajit compatible, do what I did and use Lua 5.1 for the scripting engine. This way, if luajit performance is ever needed, scripts written with my engine will not break. Lua 5.1 + bit32 (because, yes, I like to be able to do bitwise and/or/xor stuff) is my current favorite scripting language.
- 41b696ef1113 5y agoI wonder if maybe it was a good thing in the end. Lua51/LuaJIT are now dead languages - no new design changes are going to happen. It is now a stable development target for which libraries can only accumulate.
- 1vuio0pswjnm7 5y agoCould have sworn I saw this submission 4 days ago. https://news.ycombinator.com/user?id=Svetlitski https://news.ycombinator.com/user?id=Svetlitski The timestamps next to the two oldest replies make it seem like they were more recently submitted, too. But I think they are 4 days old. Seems like the title should be "Lua: Good, Different, Bad and Ugly Parts" as there is a fourth section, preceding "Bad", in the blog post called "Different". Language wonks can endlessly dismiss Lua as a programming language, but it continues to be "embdedded in", i.e., used to extend, useful network applications, e.g., dnsdist, haproxy, nmap. To learn to use the application to its fullest, one has to learn some Lua.
- josephcsible 5y agoThat's the second chance pool: https://news.ycombinator.com/item?id=11657576 https://news.ycombinator.com/item?id=11657576
- deleted 5y ago[deleted]
- anonymoushn 5y ago(2012) "lua 5.2 has no globals" is a bit of a misleading takeaway from the getfenf/setfenv changes in lua 5.2.
- dang 5y agoWe've added the year above now. Thanks all!
- monatron 5y agoI work on a project that leverages Kong's API Gateway, which is essentially Nginx + Openresty (Lua) + Kong (More Lua). The killer feature wrt Kong is the plugin ecosystem, which (among other things) allows you to act on the request/response lifecycle at various stages. Developers coming onboard to the project usually have little to no experience writing Lua, but we've found that coming up to speed on the language and it's runtime to be fairly painless. These days Kong has shims to write plug-in code in a few different languages (javascript, python, go, and more recently a wasm runtime) but despite our teams unfamiliarity with the language we still go back to Lua because performance can't be beat.
- JonChesterfield 5y agoA missing 'good' - the reference implementation is extremely easy to extend with existing C or lua libraries. E.g. embed sqlite.c and bindings to it and all of penlight (a bunch of standard library like utilities written in lua). There are some package managers. However concatenating all the source plus a bunch of libraries into one .c file also works great, My bootstrap/install is to clone that one file via git and feed it to clang.
- jonpalmisc 5y agoI’ve had a very positive experience using Lua as an extension language. I was writing a text editor at the time. I wanted as much of the core code/actions to be written in Lua as possible, as I’ve always disliked very thin scripting APIs that sit on top of opaque native procedures. I was able to wrap a handful of native functions in Lua code, then write the remaining 85% of the editor core in Lua. Everything was very fast and the process was straightforward. I’d definitely choose it again.
- jbotz 5y agoThis is from 2012 (should say so in title).
- BoogerDan 5y agoLua appeals to my sensibilites and I want to make use of it. It seems to be a well-designed language and it has a very performant JIT. But a big weakness for non-embedded use cases seems to be its ecosystem. A recent little project I wanted to use Lua on included a sqlite database, oldschool 3DES encryption, and an SMTP client. For many languages, there would be a clearly mob-approved library for each of these. But I found 3 or 4 possibilities to use sqlite in Lua, with no clear winner. I ended up using Ruby instead.
- strenholme 5y agoWhile what you went through and felt is valid, I was able to find, in LuaRocks, libraries or Lua interfaces to libraries implementing everything on that wish list. In more detail: a sqlite database https://luarocks.org/modules/tami5/sqlite https://luarocks.org/modules/tami5/sqlite https://luarocks.org/modules/dougcurrie/lsqlite3 https://luarocks.org/modules/dougcurrie/lsqlite3 3DES encryption https://luarocks.org/modules/starius/luacrypto https://luarocks.org/modules/starius/luacrypto https://github.com/somesocks/lua-lockbox/blob/master/lockbox/cipher/des3.lua https://github.com/somesocks/lua-lockbox/blob/master/lockbox... an SMTP client https://luarocks.org/modules/luarocks/lua-smtps https://luarocks.org/modules/luarocks/lua-smtps
- astrobe_ 5y agoIn my limited experience, when you want to interact with C libraries, you often end up reading the docs of the original C library anyway because often the bindings are nearly 1:1 - and often the trouble lies in the word "nearly". Just writing yourself the bindings you actually need is most likely the best approach. It should be relatively easy to do, because by using something like Lua you goal is precisely to expose functionalities implemented by your language as Lua functions (otherwise you would just convert them into dynamic library functions and use a classic glue language with a decent FFI to connect them). This is the value proposition of an embeddable scripting language at heart.
- ufo 5y agoLua 5.4 now includes a short utf8 library in the standard library. It has ways ways to get the utf8 string length and regex patterns for utf8 codepoints. However, it doesn't include any functionality that would require large tables of characters (for example, knowing which unicode characters are alphabetical). ----- The requirement that return be the last statement is to avoid a syntactic ambiguity. Since Lua has no semicolons, there would be no way to know whether a line following the return statement is part of the return statement. return f()
- delusional 5y agoWould you not just terminate the return when you see a newline? like the language already does for expressions.
- ufo 5y agoNewlines are not significant in Lua. From Lua's point of view, there is no difference between return f() and return f() You can also put multiple statements on the same line with no semicolons x=x+1 y=y+2
- delusional 5y agoI didn't know statements didn't need to be on separate lines. That's really cool. Thanks for the correction!
- abdusco 5y agoFrom a language design perspective, is it easier to build a compiler that considers `;` or newline as the end-of-statement operator, or one that works like Lua, where statements can appear anywhere?
- ufo 5y agoFrom a language design point of view, the hard thing about newline as a statement terminator is how to deal with expressions that span multiple lines. In shell you have to use explicit line continuations. In Python, the line breaks are ignored if they are inside parenthesis (which is one of the reasons why it doesn't allow multi-line lambdas). Javascript deals with it by having an ad-hoc semicolon insertion algorithm. The way Lua does it is closer to what you might be taught if you take a compilers class. It's just a plain old context free grammar. The tricky bit was designing the grammar so that semicolons were not needed. You have to be careful when there is something that could either be parsed as a continuation of the previous statement or as the start of the next statement. One place where Lua does this is in the rule that the return statement must be the last in the block. Another is that not every expression can appear as the function part of a function call. For example, something like 1() is a syntax error because the grammar doesn't allow function calls where the function is a numeric literal.
- leephillips 5y agoAt first Lua seems strange, but after a while you start to appreciate it. It’s designed with a small number of concepts that manage to lead to expressive code with excellent performance. I use it for two things: scripting TeX¹, where it allows you to do amazing things², even complex numerical calculations³ within the document; and writing Pandoc filters, where it is now the standard method. 1 ‘LuaTeX comes of age’. LWN. Available from: https://lwn.net/Articles/731581/ https://lwn.net/Articles/731581/ 2 http://wiki.luatex.org/index.php/TeX_without_TeX http://wiki.luatex.org/index.php/TeX_without_TeX 3 http://www.unirioja.es/cu/jvarona/downloads/numerical-methods-luatex.pdf http://www.unirioja.es/cu/jvarona/downloads/numerical-method...
- ludamad 5y agoI was a huge Lua fan, but once I jumped on the typescript bandwagon I found the lack of (production ready) desugaring and static type compilers for Lua to be a negative. The language is fantastic, but what I expect from a scripting language is always expanding, I guess
- MapleWalnut 5y agoWhat about https://github.com/Roblox/luau https://github.com/Roblox/luau? It gives Typescript-like annotations.
- ludamad 5y agoWith roblox behind it, I'd indeed consider it for my game. I'm guessing no support for LuaJIT, though?
- wildbook 5y agoIt's a custom fork of plain Lua, not LuaJIT, so indeed there's no LuaJIT support. They first open sourced it this November and supposedly they're planning to implement their own JIT. There's no timeframe or estimate for that though, just mentions of plans to here and there.
- erwincoumans 5y agoIt is nice small, so Lua even ran on Playstation 3 SPU! We used to ship lua binaries for the premake build system. For me, the indexed from 1 rather than 0 is a big turn off. Roblox has the mlua fork, with increased performance and added type annotations: https://github.com/Roblox/luau https://github.com/Roblox/luau
- diego_moita 5y ago> For me, the indexed from 1 rather than 0 is a big turn off. I can't understand why people complain about that. If you're using 'pairs(table)' iteration then the index doesn't matter. If you're accessing indexes directly you can have the index '0' in a table.
- wheybags 5y ago> If you're accessing indexes directly you can have the index '0' in a table. Sure, you can, but in practice you won't. For example, something like `local pos = {x, y}`. You now have to use 1/2 to access the x and y coords. Making the same table with 0 indexing is awkward and not idiomatic.
- kdheepak 5y agoYou can also write rust ( or go, or write in any other language that allows you to expose a C ABI ) and make a Lua module. This solves a number of the different, bad and ugly issues in my opinion. For example, I think using rust’s chrono or unicode_segmentation library makes life so much easier than having to deal with that in Lua. Neovim embeds lua 5.1 jit, and it’s possible to write plugins in rust for neovim using this mechanism. For any one that knows what I’m talking about it should be obvious how to do this. If not, I wrote about this more here, in case you are interested: https://blog.kdheepak.com/loading-a-rust-library-as-a-lua-module-in-neovim.html https://blog.kdheepak.com/loading-a-rust-library-as-a-lua-mo...
- daneel_w 5y agoPlenty of good bullet points, but it's now a quite outdated article. A lot has happened in Lua since 2012.
- dang 5y agoA couple past threads: Lua: Good, bad, and ugly parts - https://news.ycombinator.com/item?id=6616817 https://news.ycombinator.com/item?id=6616817 - Oct 2013 (19 comments) Lua: Good, bad, and ugly parts - https://news.ycombinator.com/item?id=5348513 https://news.ycombinator.com/item?id=5348513 - March 2013 (110 comments)