lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



So does Lua work by delaying allocation until an _expression_ needs it to be resolved, or does it allocate during assignment?

Allocations occur when executed, since Lua is an imperative language.

>No, Lua DOES use tables as the only AGGREGATE type, but things like strings and numbers are not tables. 

Why is that? The grammar and expressiveness of tables more or less allows you to define all the properties of a type within the table itself. I understand that we need to have static representations of value, but it seems like the concept of storing that data into a variable is analogous across all types, including tables. (And don't the static values just go into the vTable?)

aTable = 4
bTable = Function() return 4 end
cTable = {__call = Function() return 4 end, __index = Function() return 4 end }

Conceptually, these things are all the same- they only differ in the syntax to access the data. But if I want to add a special property to a specific integer, I have to encapsulate it in another table. Ie,
aTable = 4
aTable.isEven = true
Wouldn't work unless it was a table, which it isn't, so it doesn't (?).
aTable = {__call = function() return 4 end, isEven = true}
I'd have to do something like this, but then it isn't going to work with integer operators. I could define an Integer archetype (or prototype? What are they called >_<) that has a metatable analogous to integer functionality- and that would let me do the trick above, but I'm confused why that isn't already the case.

There ARE languages which do some of the things you are discussing, but one reason Lua doesn't is a very pragmatic one: it's hard to make such languages efficient (in size, compile speed and runtime speed). A number in Lua is stored compactly and does not incur any memory-management overhead to speak of, while a table-driven model is FAR more expensive. You are correct that Lua stores "globals" in a table, and in fact it is well-known that Lua access to locals is more efficient than global access, which is why you will sometimes find this idiom in Lua:

local foo = foo

Which copies a global into a local for faster subsequent access.

I think I'm beginning to understand. I had the impression that Tables are Expressions and Expressions are Tables in a way that's similar to particle-wave duality, and that really excited me, but it looks like it isn't quite that abstract. It seemed like you could define your own grammars and keywords within the idea of meta-tables to achieve an almost unparalleled degree of expressiveness (supplemented with ::= ofc)- and in many ways that is true as it is, but I don't think it works quite in the way that my head interprets it. With that in mind, is what I'm talking about make any conceptual sense? Could that work as a programming paradigm?

If you go down this path you are creating a language much more focused on meta-pogramming, and this cane get very complex very quickly. In some ways, much of the complexity of C++ is directly traceable to its attempt to be both a general purpose language and a meta-language. I think it's a tribute to the Lua designers that they created a language that is both compact and yet still gives 80% of a meta-language (and, imho, the other 20% is the "dangerous" 20%).


Thank you so much! Your replies are awesome!

yvw.

--Tim