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

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: