Hacker News new | past | comments | ask | show | jobs | submit login

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)




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

Search: