29 ms·
A Look at the Design of Lua
- epse 8y agoThe images appear to be dead..
- russellbeattie 8y agoAs a developer, I've always liked languages that stress maps/hashes/dictionaries/tables/associative-arrays as their man data structure. Off topic, but I've always thought that JavaScript and JSON would have been a lot better without the relatively artificial distinction between objects and arrays. Now that there's object shorthand and destructuring, this will never happen, but it was a nice thought for a while.
- chmod775 8y agoNobody is stopping you from just using arrays for everything - remember they're also objects. const a = [2,3,…] a.prop = "hi" Now whether that's actually something you should be doing is a different matter...
- russellbeattie 8y agoI was thinking more along the lines of using "objects" for everything like Lua: let foo = {'a', 'b', 'c'}; . (Also on my JS wish list: Killing the use of const for anything besides actual constants.)
- anonytrary 8y ago> Killing the use of const for anything besides actual constants You should be using const for everything besides variables that need to get re-assigned.
- enriquto 8y agoso, in a saner language, variables would be const by default
- russellbeattie 8y agoI just recently learned Rust's 'let' works that way, unless you add the 'mut' keyword as well. (And const, of course, is always immutable.)
- russellbeattie 8y agoHeh, I like how you carefully worded that, because normally 'const' would signify that a variable is immutable in just about any other programming language in the world. Except in JS, where const was awkwardly jammed into the language. I guarantee if the spec writers had known how badly const was going to be abused, they wouldn't have added it. If you want a strongly typed language, go use C or Java and stop making JavaScript incomprehensible to all but the most pedantic developers.
- zbentley 8y ago> normally 'const' would signify that a variable is immutable in just about any other programming language in the world That's not true in Java, C, Ruby, or Perl (with the 'constant' prag-module), not really true in C++ (unless you're careful), and . . . generally just isn't true in most languages. While a few languages do add recursive immutability properties to their equivalent of 'const', most languages use the idea of a 'constant' only as a means of prohibiting name re-binding, not data modification. And even then, many languages that only prohibit const re-binding only do so in an optional (i.e. it's a warning) way. Many of those same languages do support immutable data structures either out of the box or via popular libraries (though in non-compiled language, such structures often come with a performance cost). They just don't call them const[ant]s. Edit: I'm also curious about how you think 'const' is being abused in JS. I'm not an expert in the language--this is a genuine question; not a veiled disagreement.
- anonytrary 8y ago
- hamandcheese 8y agoThe distinction is syntactically important for JSON, IMO. Nobody wants to write integer keys for each element in an array.
- trynumber9 8y agoYou don't have to { 1, 2, 3, Prop = 'Example' } is the same as { [1]=1, [2]=2, [3]=3, ['Prop'] = 'Example' }.
- trynumber9 8y agoExample: https://repl.it/repls/ConcreteIntelligentBootstrapping https://repl.it/repls/ConcreteIntelligentBootstrapping
- kybernetikos 8y agoThe javascript spec makes it clear that an 'array' is just an object with a magic length property that is always one bigger than the largest set numeric index. The array syntax can be thought of as a shorthand where you want the indexes inferred from order instead of having to list them out. There are obviously a few useful methods that you use on arrays but almost all of these will work on any object with a length if you .call or .apply them.
- nly 8y agoSpeaking of JavaScript. For a lightweight, Lua-like, embedded JS runtime there's always Duktape https://duktape.org/ https://duktape.org/
- kibibu 8y agoYou can always use PHP! A PHP array works as a sequential or an associative array; or both at the same time
- heavenlyblue 8y agoYou can also use a donkey as a car.
- deleted 8y ago[deleted]
- wcrichton 8y agoLua is a big reason why I'm interested in both systems and PL today. I started using Lua to build game mods in high school, and I was struck by how simple it was compared with Java, or even Python that I had tried learning earlier. The all-encompassing "everything is a table" paradigm really sparked my interest in the design of systems of computation, and how a broad range of programing patterns could be expressed with a few primitives.
- gvx 8y agoYou might find Isle interesting (https://github.com/gvx/isle https://github.com/gvx/isle). It is a small programming language I've designed and implemented a couple of years ago heavily inspired by Lua's "everything is a table". In it, a function call looks like a name concatenated to a table literal, and calling a function actually consists of creating that table and using it as the environment for the function. I still think it contains some neat ideas and concepts, even though I would never actually use it for something practical.
- Immortalin 8y agoFor those of you who are interested, I run a Lua newsletter. The Lua community is small so every bit helps :) Shameless Plug: https://Luadigest.immortalin.com https://Luadigest.immortalin.com
- anilakar 8y agoStay away from Lua and use Python, Go, Node, Rust or even C in new projects. This is going to be a lengthy rant for a comment; apologies in advance. Having written a ton of networking code in Lua I can tell it has a lot of design defects that most other programming languages have avoided. Especially the deeply embedded one-based indexing and lack of a separate integer type (chars are strings, ints are floats) make it wearing to type any code that has to parse any binary data. Even bit manipulation requires going through the FFI. Considering that Lua is often touted as the optimal lightweight solution in small embedded systems, these features are must-haves. The builtin string pattern matching library attempts to mimic regular expressions, but bites the user in the leg every time they attempt to use it for anything remotely regex-y. Unfortunately most usual string manipulation seems to revolve around them and the string.gsub function. The inconvenient string library is also largely irrelevant. The developers call their language eight-bit transparent which in reality means that the programmer needs a separate Unicode library, rendering the whole standard string library mostly useless. One cannot even format UTF-8 text, because the string library doesn't differentiate between characters and bytes. The official documentation mostly revolves around C bindings. The actual Lua language documentation is provided by another web site that looks like it has been written by an attention-deficit child. Instead of getting to the point, the articles often first provide a list of ways on how not to implement things, or they are actually benchmarks of fifteen different ways to call the standard library to get the same effect. Sometimes, the provided code does not even work – for example, the given XML parser code crashes when it hits one of the error paths. The small runtime and fast VM do not really matter when the language itself is lacking and requires a metric ton of external libraries to achieve even the simplest things that are possible in standard C and Rust, which provide the same level of performance and better type safety.
- saagarjha 8y agoC and Rust solve different problems. They’re compiled, for one.
- anilakar 8y agoC and Rust solve exactly the same problems as any other programming language in the embedded context: they try to interface with real world. Whether they're compiled or not rarely matters, when software updates are done by reflashing the whole device. Edit: If you have a device with enough computing power to run a scripting language interpreter, you can use Python in most cases.
- kzrdude 8y agoSince the lua authors wrote this article, it can't bring much of a new perspective on Lua
- Uhrheber 8y agoI always perceived Lua of being completely weird, and asked myself why people would ever want to use it. Especially nowadays, where embedded Python is everywhere.
- anilakar 8y agoOn modern embedded hardware, Python is a better alternative in practically every case. If size and computing power are an issue, either investing in better hardware or spending time writing C yields better results.
- AHTERIX5000 8y agoC solves different problems, there are cases where scripting languages can be extremely helpful yet running under heavy RAM constraints. Lua and JS can be adapted to quite low-mem situations (we were using on 128 k) and there are no comparable & production tested Python implementation that can do the same.
- saagarjha 8y ago> On modern embedded hardware, Python is a better alternative in practically every case. How so? Do you have an example where Python outperforms Lua or uses significantly fewer resources?
- enriquto 8y ago> I always perceived Lua of being completely weird, and asked myself why people would ever want to use it. It depends on your habits. I for one prefer Lua to Python and find it much more pleasurable to code on. There are some things in Python that I like (like the blocks defined by indentation), but overall the language seeems too complex for no reason when coming from lua.
- jokoon 8y agoI'm planning to use tcc as a scripting language. Not sure if it's a good idea, but as long as I don't have to rebuild my code, and as long as it's not slow like most scripting languages, it sounds like it's good enough. Although I agree it might be difficult to write scripts in C, in the end it might just depend on the core functions that are exposed. I'm not a fan of C, I prefer python, but I guess I will have less issues when interfacing c code to c++. Python seems quite fat, slow, and too much different than C++.
- antirez 8y agoI've the feeling that Lua became popular mostly based on its implementation, fast, small and self-contained portable C code, and not for the merits of the language itself. Certain things are addressed in Lua 5.3, but in general Lua has several flaws: - Before Lua 5.3 there was no integer type, this makes a lot of things harder than needed in programming, unless you just write high level code. - Lua trivially departs from standard syntax like ==, !=, zero-based indexes, without good reasons, creating a general feeling of confusion. - There was an odd idea about what to include by default and what was optional. The lack by default of bit operations makes the language always in need of a trivial library. - Tables as a single data structure are not ideal for many tasks. Especially the dualism between tables that are indexed by sequential numbers and tables acting as maps. It's a lot more kind to your users to give up with the solely academical ideal of "just one data structure", and give programmers at least an ordered and a map data type, or at least come up with something better than that. Lisp attempt at doing most things with lists was much more successful and clean. - The stack-based C API is not comfortable to use. I could continue to be honest, for instance by default you can't type an expression in the REPL and see the value returned. However when the task at hand is to write small scripts for a larger system, the implementation of Lua offers big enough advantages to make it a worthy choice.
- enriquto 8y ago> I've the feeling that Lua became popular mostly based on its implementation, fast, small and self-contained portable C code, and not for the merits of the language itself. And this is quite fair. Those are fundamental qualities of a new programming language.
- patrec 8y agoI don't think that's a completely fair assessment. It's true that the language has some gratuitous feeling warts (not being zero based is a very severe flaw where embedding into C/C++ apps is a primary use case; it also messes up luajit and terra's UX no end, global by default is another problem, albeit one that can be mitigated with tooling). But what other language packs so much punch for so little weight? Despite being quite simple, the syntax still is expressive enough for a lot of DSL uses. Lua has proper tail recursion and an elegant co-routine design and it's slightly idiosyncratic lpeg library is worlds better than the regexp offerings of most languages. It has serious meta-programming power based on a much simpler model than e.g. python or ruby. Despite its warts lua still feels like a better (imperative, statement-based) scheme than scheme to me. What scheme offers a similar combination of speed and expressiveness for the same conceptual and file-size weight as lua(jit)? Wrt to repl: the torch7 repl is quite serviceable, so it's worth giving it a try even if you're not into deep learning.
- deleted 8y ago[deleted]
- imhoguy 8y agoTo courious web nerds: currently both Nginx and HAproxy have got Lua scripting modules to extend themselves by implementing e.g. customized filtering, routing, caching, proxing, storage etc. One can even build a full featured web app backend in embedded Lua tied to Nginx.
- fosk 8y agoDisclaimer: I am CTO of Kong. If you want to look at a large OpenResty/Lua application from a code perspective you could take a look at Kong[1] - which is the most popular public codebase built on top of NGINX/Lua at the moment. CloudFlare also uses the same stack but most of their codebase is proprietary. What really makes Nginx/Lua a good combination is primarily LuaJIT[2] support, a much faster implementation of the Lua VM. [1] - https://github.com/Kong/kong https://github.com/Kong/kong [2] - http://luajit.org http://luajit.org
- sgt 8y agoWhat made you choose Lua instead of say, embedding Python into nginx? I can think of a few good reasons like embeddability, code size, simplicity but it would be interesting to hear your take on this.
- ufo 8y agoLuajit is pretty fast
- minxomat 8y agoCan't speak for Kong, but I'm currently working on creating a microservice in lapis (using moonscript). The speed of the underlying OpenResty server, as well as lapis ergonomics make this really fun. I guess the only practical reason I can see is the async everything without clutches approach, as well as the truly impressive TTFB. For me, day 1 productivity is really high, comparable to Nancy, the framework I was using before.
- tybit 8y agoRedis and Envoy also have the same. It seems to be the scripting lingua franca for interacting with native code.
- mion 8y agoI studied at PUC-Rio and I met Roberto Ierusalimschy (the main author of Lua) when he was teaching a course on semantics. I once asked him if I could send a PR about a feature I wanted in the language and he said something that I never forgot: "Yes, but I won't use your code. I love that people send me ideas, but I actually enjoy coding... so I will gladly take your suggestions, though I will write it myself." He then explained this "dictatorial" behavior is what allowed him to keep the implementation simple and concise over the years. At the time he boasted about the source code being less than 8K LOC, though it has likely increased in recent versions. (I think it was around version 3.) I'd recommend taking a look at the source code, it's truly a C masterpiece.
- gota 8y agoI'm currently a PhD candidate at PUC-Rio. For context, there's a 'meet the researchers' round of seminars during the first semesters mainly targeted towards Master's students (so they can choose an advisor). Prof. Ierusalimschy's was one of the most interesting seminars in my first year. His passion is apparent in the way he talks about Lua's history and current development. Unfortunately I haven't taken any courses with him As a side note, it's nice to see folks from around here on HN
- cmsj 8y agoFWIW Lua is somewhere around 15,000 LOC these days.
- weeksie 8y agoI second that recommendation about the Lua source code. Back in the 00s I built a kiosk management system in Lua, C, and C++ and it was a delightful language to work with and I found myself reading through the sourcecode a fair bit, always impressed.
- kzrdude 8y agoLua is a good example of an open source project that is not an "open collaboration", isn't it.
- senderista 8y agoWow, I’ve never met any faculty with the time to write code for fun.
- mitchtbaum 8y agoWhat Lua lacks in terms of performance and broad-scale, low-level library integration can be made up for within the Rust ecosystem where these exist along with a well-built Lua wrapper, rLua.
- tonetheman 8y agoFor game development lua is wonderful. Pico8 uses lua as does love/lua. Both are wonderful tools to write games.
- upofadown 8y agoOf particular note; LuaJIT can pretty much directly call C libraries. From here: * http://luajit.org/ext_ffi.html http://luajit.org/ext_ffi.html local ffi = require("ffi") ffi.cdef[[ int printf(const char *fmt, ...); ]] ffi.C.printf("Hello %s!", "world") So while, say, Python is "batteries included", LuaJIT is "all the batteries". Using C libraries in regular Lua is fairly easy as well, but LuaJIT is exceptional.
- Sean1708 8y ago> So while, say, Python is "batteries included", LuaJIT is "all the batteries". Do you say this because LuaJIT can call shared libraries dynamically? If so, what about ctypes[0]? Is LuaJIT's FFI just easier to use, or does it do something that ctypes can't? [0]: https://docs.python.org/3/library/ctypes.html https://docs.python.org/3/library/ctypes.html
- ufo 8y agoThe two most appealing features of the LuaJIT FFI: 1) The syntax. To specify the types of the FFI functions you you use a DSL that is similar to C declarations. Often you can copy paste function prototypes from ".h" files with very minimal modifications. With ctypes you need to specify the types with Python function calls. See "ffi.cdef": https://luajit.org/ext_ffi.html https://luajit.org/ext_ffi.html 2) Performance. If your code is inside a JIT-compiled trace, LuaJIT FFI calls have minimal overhead. They compile down to something very similar to what a C compiler would generate. Ctypes, on the other hand, must "interpret" the FFI calls which adds more runtime overhead (although admittedly this is also true for LuaJIT if your code ends up outside a compiled trace).
- SeanLuke 8y agoIt's worth mentioning the debt Lua owes to Self and Newtonscript. An extraordinary amount of Lua's syntax and semantics is directly lifted from these languages. Newtonscript in particular seems to be a template for a great many of Lua's interesting features. What drove me away from Lua is its philosophy of providing minimal tools in which it is possible, but never easy or convenient, to do work in various programming modalities. Polymorphism, namespaces and packages, arrays, unicode... these things all can be done, but require significant head-standing and in many cases a lot of boilerplate. Even proto-style OO, which you would imagine would be Lua's signature feature, involves lots of rigamarole compared to languages (like Newtonscript) where it is baked in and elegant. I kept wanting Lua to pick a pony. Throughout my usage of Lua, I've consistently gone back to Lisp, where I could change the language to make inconvenient things convenient when I needed to.
- chaoticmass 8y agoMaybe it's because I've mostly used Lua as a tool in my homebrew game engine side project, but what you said about their philosophy of providing minimal tools where it is possible to work in different programming modalities is one of my favorite things about Lua.
- eternalban 8y agoRoberto Ierusalimschy, et al., on "The evolution of an extension language, a history of Lua (2001)" [1], is an interesting read. The word "configuration" makes repeat performance in that paper, but oddly is entirely missing from this 2018 paper (by the same authors). [1]: https://www.lua.org/history.html https://www.lua.org/history.html
- xte 8y agoWhile I'm not a developer nor I like lua I'm curious about a thing: what's the difference between for instance Guile scheme? Because they seems closely similar to me and Guile is far more clear and effective, at least for my programming skills...