lua-users home
lua-l archive

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

> This is performed by sending the 'this' object as upvalue to all the
> methods. It may give a slight runtime penalty but I doubt it's even
> measurable. Can show you sample if you are interested, but the technique has
> been mentioned on the list before.

Just a thought on that technique:

This works and has some upsides, but it prevents anyone from reusing
functions and also comes at the cost of having one function per
object. If you have say 10 methods and 1000 objects, you generate
10000 functions. This penalty is actually somewhat measurable in terms
of memory usage and might also cost the GC a bit. Though it might be
allowing even faster calls since upvalues avoids you one or two table
lookups (table[k]=>metatable.__index[k]=>function vs.

But performance is usually not the first issue if it's not a strong
constraint for your environment. Versatility and flexibility is
usually for me a constraint of general importance and I found that
functions that are using upvalues for referencing the self table are a
bit less versatile since they can not be reused (or abused - another
constraint in your environment could be that exactly this is not
wanted ;)). On the upside, it's much more simple to pass mere
functions with their upvalues around and store them, since it's not
required to bind them to their object (e.g:
   local f = obj.f
   local f = obj.f

So it's more or less: You should actually know what you are doing ;) -
and the : and . semantics that is often not understood by beginners is
actually a hint to them to get a clear head what all this is meaning.
I learned more about the backgrounds on programming when learning Lua
than I learned when using Java, C, Javascript or PHP - simply because
I had to develop an idea what's going on there on the language level.
Actually, I could even transfer my experience much more often from Lua
to other languages than the other way round.

In short: I am unsure if it isn't better to confuse people (and make
them learn something) than trying to do what they might expect (just
to be confused later on).