6 ms·
Haven't used Lua much but the concept of tables is really neat. Its simple, orthogonal and the fact it lies at the core of many features in Lua (composite data
by shaneeb 14y ago
Haven't used Lua much but the concept of tables is really neat. Its simple, orthogonal and the fact it lies at the core of many features in Lua (composite data types, metaprogramming, etc) means those features are neat too.
- mscarborough 14y agoWhat do you mean by orthogonal in terms of simplicity and similarity to other Lua features?
- shaneeb 14y agoOrthogonal in the sense that the same concept is used if you need maps/arrays or for namespacing functions or metatables(for metaprogramming). Its like a "core" concept that binds seemingly different features in an coherent way. Havent seen a concept used this elegantly in any other language
- gruseom 14y agoLists in Lisp, objects in Smalltalk, arrays in APL, stacks in Forth. Lua keeps first-class company.
- Shorel 14y agoGo back to Linear Algebra class.
- ubercow13 14y agoThat was unnecessary. The meaning is the same.
- vor_ 14y agoI find Lua's use of tables as a universal container to be clumsy. For example: local array = { one, two, threee, four } A gap is silently created in the array because Lua returns nil for undefined variables. This causes the (#) operator to an return incorrect result and can lead to subtle, difficult-to-find bugs in the program. Lua makes no distinction between nil and a non-existent element, so assigning nil to an array element will also create a gap, rather than shifting the keys of the subsequent elements downward: local array = { 1, 2, 3, 4 } array[2] = nil -- array is now { [1] = 1, [3] = 3, [4] = 4 }
- gruseom 14y agoYour second example is not "clumsy", but a consequence of how Lua's tables are simple and cleanly designed. It would be weird magic for the elements to suddenly shift downward. One can do that oneself if need be. Your first example has a point – perhaps undefined variables should error when accessed – but that's got nothing to do with tables.
- vor_ 14y agoArray keys shifting downward wouldn't be weird magic; it would be the expected behavior when removing an element from an array. Dismissing it as simply how Lua's tables are designed doesn't make it intuitive or expected. > Your first example has a point – perhaps undefined variables should error when accessed – but that's got nothing to do with tables. Well, of course it does. The point is that you can silently end up with array "holes" due to a typo, which you are supposed to avoid in Lua for a number of reasons, such as an the length operator returning an incorrect result. I don't understand why my previous comment is getting voted down for pointing this out.
- groovy2shoes 14y agoIf you want the array elements to shift into the gap, you want to use table.remove: local array = {1, 2, 3, 4} table.remove(array, 2) -- array is now { [1] = 1, [2] = 3, [3] = 4 }
- stcredzero 14y ago> Havent seen a concept used this elegantly in any other language Object in Smaltalk.
- FreezerburnV 14y agoI agree about the tables. Personally, I find Lua to just be a more honest language, in a sense. Languages like Python or OCaml really just use tables under the hood for objects and what not (from what I understand), but Lua shows you that outright.
- judofyr 14y agoIt's really annoying when you're working with JSON though. I've used some Lua libraries (the OpenResty stack) where it's close to impossible to generate "[]" because there's no difference between arrays and hashes (and thus an empty table gets converted to "{}").
- justincormack 14y agoWell JSON does assume JavaScript semantics, so this type of issue crops up a lot with lots of languages. You should be able to define an arbitrary value that is used as [] by the library fairly easily, or a property of the table.
- ludwigvan 14y agoHow was your experence with OpenResty btw? It looks like a really interesting project.
- judofyr 14y agoI think it sorely needs a framework on top that handles and generates the nginx.conf. There's quite a bit "hacking" to get every piece to work with each other (e.g. to talk to a database in Lua it needs to be configured in nginx.conf). But all in all it's surprisingly effective.
- akavi 14y agoHow is this different from objects in javascript? (Tangentially, I don't agree that conflation of array and object is a good thing)