lua-users home
lua-l archive

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


>From a strictly Lua-the-language point of view, here are some trends
I've seen, in various material I've read.

Warning: generalizations abound!

#1 - "Closures are unimportant"

Often, somewhere early in the book or tutorial or whatever, you're
given a little example, probably a variant of
making a counter, as in PiL. And that's the last you'll ever hear of closures!

Down the line, you might see some functions which have local functions
as upvalues; in these cases it often feels
like even odds that the author just assumes functions are more special
than they are (q.v. point #5). But rarely is
there any deliberate use of variable capture to build up a complex
system (e.g. versus classes, as other posters
have touched upon), even just something like two or three closures
that operate together on one or more shared
upvalues.

#2 - "Metatables are for OOP, that's it"

You'll get the "let's make a class" section. Maybe you'll see proxies
later. And that might as well be metatables'
entire purpose in the language.

#3 - "Weak tables don't exist"

These just never seem to show up.

#4 - "Coroutines are unimportant / too hard / etc."

"Oh yeah, Lua has coroutines. Sounds pretty powerful, right? Yes, they
are! Well, on to the next topic." And even
that might be generous. :)

#5 - "Functions are super special"

Basically, there's a lot of this:

local function MyFunctionThatGetsUsedOnceEver ()
  -- Do something great
end

function M.Init ()
  timer.performWithDelay(1, MyFunctionThatGetsCalledOnceEver)

  -- Do other setup; the above timer will be called on the next frame
end

versus just instantiating the local function on the spot in M.Init(),
i.e. timer.performWithDelay(1, function() -- stuff end).
Obviously each style has its place, but it often seems like the first
is preferred to the exclusion of the second, to the
point where readers might not even realize the latter's
possibility.[1] I'm not sure it quite clicks that the first case just
assigns a function-type value to a variable.

#6 - "The garbage collector will never give you problems"

This would probably be out of place in most of the material I cite,
really, but the intermediate/advanced readers you
say you're aiming for may be a different story. Once you have
something of a larger scale going, you might need to
take an active hand with the GC (and maybe the allocator): tuning
parameters, temporarily disabling the GC, etc.
This also brings up the issue of data recycling (tables, closures, et
al.): how to do it, when it's worth it, when not to
bother.

#7 - "Debugging is easy"

There's a lot one can cover when it comes to debugging strategies,
error diagnosis, and the like, but often I've seen
author just mention, say, "Put a print() statement here", and call it a day.

Of course, often the above are just fine where the focus of the book
or article is on, say, an SDK and not Lua itself.
They seem to me rather more conspicuous otherwise.

[1] - And then they need to waste their creative juices coming up with
succinct names for one-liners. :)