Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

may you please elucidate why you dissuade the use of lua ? thank you kindly!


Lua is a language that I started programming with, and has a special place in my heart even if has some crappy parts.

The main issue I have with the language is with table accesses and 'nil'. Tables in Lua are the most fundamental type of object in the language, it can be either an array (1-based index) or a hashtable. In this language objects are basically just tables with fields in them, and with metatables you can basically emulate all the features in a typical OOP language (classes, inheritance, traits, operator overloading, etc.) Field access in objects are just table accesses (like what you can imagine with Javascript).

However, when you try to access a field in a table that doesn't exist (such as 'print(table.key_that_doesnt_exist)'): no errors or exceptions are explicitly raised, it just silently returns nil. This is such a dealbreaker that makes the language much harder to debug than other languages (at least Javascript returns undefined, which is different from null! Oh well, that actually has problems of its own though....) Some more horror: global variables are also implemented as tables (imagine that there's a table _G at the top of the scope). This means that any spelling mistakes with variables will also just silently return nil, since if it doesn't find any variable names at the local scope, it tries to find at the global scope.

The global variable thing was actually such a big problem that people came up with some voodoo metatable trickery script like strict.lua (https://github.com/deepmind/strict/blob/master/strict.lua) that prevents this from happening (a showcase of the language's power, but probably not its proudest). I'm sure you could also do this with table creation (make a wrapper function tbl() that automatically adds metatables to tables to prevent invalid field access, so every time you need to do things like 'pos = tbl({x = 1, y = 2})') But still, there isn't a solution baked into the language, and it's cumbersome to do this (and I'm not sure about the performance implications of this addition).

Right now I'm trying to integrate Squirrel (http://www.squirrel-lang.org/) instead of Lua as a scripting language into my game. Squirrel is an embedded scripting language that's hugely inspired from Lua, but also fixes this shortcoming of the language by making invalid table accesses runtime errors. And when you want to add new fields to a table you need to explicitly do so via the <- operator:

    table = {}
    // print(table.x) (Runtime error)
    // table.x = 1  (Runtime error)
    table.x <- 1
    table.y <- 2
    print(table.x) // Outputs 1
which is much more explicit and less error-prone.


Lua gives you means to build your object system anyway you want. A trivial example to protect a table below:

  local p = { x = 1, y = 2 }
  print( p.x, p.y )
  
  setmetatable( p,
  {
      __newindex = function() assert( false ) end,
      __index = function() assert( false ) end
  } )
  
  print( p.z )    -- you get assertion failure
  p.k = 12        -- you get assertion failure too


I’ve already mentioned how to do this with metatables. But you still need to wrap this in a function and call it every time, it’s quite cumbersome. And the whole thing falls apart when you start using other people’s libraries (For example, you enabled strict.lua in your codebase, but then it starts affecting other libraries which relied on the original Lua behavior… Or you’ve made your own object system, but that one library you’ve imported uses middleclass and another uses rxi.classic, and you need to go though the headache of making sure they’re all compatible)


Lua feels so rigid, inflexible compared to something like JS. If you want abstractions, and want functions to be first order objects etc, does Lua help. Does Lua have "eval" or something similar?


Yes. You can compile Lua code with `loadstring("return 1 + 1")`, which returns a function that runs the provided code. `loadfile()` does the same thing with files. You can also change how the import mechanism works to load compile-to-Lua languages or similar. There are also first class functions and functional programming libraries. I'm personally a fan of luafun.

Usually when programming Lua I can remember every detail of the language without having to consult a reference or wonder which library does what, because it's so simple. I understand that too much simplicity might be an issue for other people, though. I sometimes wish that JavaScript was as simple and easy to understand as Lua is, and Lua is also free of many of JavaScript's warts, but the lack of proper OOP support seemed to kill adoption of the language in projects like Torch. It's still possible to use Lua's prototype-based paradigms to implement OOP, but the implementation details vary depending on which OOP library you choose, and the lack of first-class support is unsatisfying for many people. For me it was more of a mindset thing, and I found I was happier settling for a limited version of OOP or eschewing it for prototype-based programming instead, as it was simpler and satisfied all my needs.


It's the automatic type coercion (strings <=> numbers), the overusage of tables as a universal data structure with bad API, the bizzare syntax.

It might as well be Perl. I don't want you to use Perl either.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: