lua-users home
lua-l archive

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


> Alexander Gladysh writes:
>  > I [organize] each file as follows:
>  >     local foo, bar, baz = import 'path/to/file.lua' { 'foo', 'bar', 'baz' }
>  > ...
>  local Corge = require 'qux.quux.Corge'
>  local foo = Corge.foo
>  local bar = Corge.bar
>  local baz = Corge.baz
>
>  There can, however, be an advantage in removing some or all of the last three
>  lines and, for example, referencing "Corge.foo" rather than "foo" in the code
>  since the prefix "Corge." indicates explicitly where the function "foo" comes
>  from, and the Hungarian-ish prefix makes obvious the fact that "foo" is external
>  to the current module.  The "local foo = Corge.foo" is called a "static import"
>  in a some other languages and its use is recommended only sparingly[1].
>
>  [1] http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html

As I understand that article, it argues against abuse of import-all construct:

  import static java.lang.Math.*;

Such abuse, I agree, is a bad thing.

But what I use is more like named import with aliases (apologies if
messed terminology):

  import static java.lang.Math.PI;

That is:

  local pi = import 'math.lua' { 'pi' } -- actually, since it is a
global table: import(math) { 'pi' }

Which (careful) use is actually praised in the article.

In case of heavy importing, of course, you may use module-name prefix:

  local math_pi = import 'math.lua' { 'pi' }
  print(math_pi)

Which is equivalent to

  local math = import 'math.lua'
  print(math.pi)

But saves a bit of performance avoiding hash lookup.

Alexander.