lua-users home
lua-l archive

Re: Autolocals?

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


luaSub's 'using' extension does the following:
-- local sin,cos,tan= using math
-- -->
-- local sin,cos,tan = math.sin, math.cos, math.tan
It would be trivial to make this use 'require'. The reason that variables are named on the left side is a technical one.
-asko
Alexander Gladysh kirjoitti 24.3.2008 kello 12:49:
When used frequently, however, it can be a performance win since it
eliminates a table lookup. I've started to use the convention:
 local Corge_foo = Corge.foo
1. I prefer to avoid using require.
2. There is no validation for Corge.foo to actually exist.
<...>
If you prefer, the "key not exist" function could be defined
apart from the module:
 local Corge_foo = import(Corge, 'foo') -- or
 local Corge_foo = checked(Corge).foo
My import() function does check for the key existance. What I meant in
the original post is that there is no protection from messing up order
of existant key:
local foo, bar, baz = import 'path/to/file.lua' { 'bar', 'baz', 'foo' }
If you write it separately, well, yes, it would arguably be somewhat
easier to spot while reading code, but at price of greater code bloat:
 local file = import 'path/to/file.lua' -- Okay, set metatable there
 local foo = file.foo
 local bar = file.bar
 local baz = file.baz
As soon as you fold it in single line for compactness, all benefits vanish:
 local file = import 'path/to/file.lua'
 local foo, bar, baz = file.foo, file.baz, file.baz
Alexander.

AltStyle によって変換されたページ (->オリジナル) /