lua-users home
lua-l archive

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


Anders Bergh wrote:

That's bad... ("foo"):len() looks weird.

It's not just a special case, though.  It's a bit of syntax
that applies to other things too: anytime something other
than a variable name or a function call is used before
square brackets, a dot, an argument list, or a colon, it
needs to be surrounded in parentheses.  (This turns it into
a "prefixexp" -- see the syntax chart at the end of the
manual.)

 > =({"foo", "bar"})[2]
 bar
 > =({foo = true}).foo
 true
 > =(function(A, B) return A + B end)(2, 2)
 4
 > =({fun = function(self, argu) return self, argu end}):fun("Hi")
 table: 0x493be0 Hi

I believe the purpose (or one purpose) of this is to
minimize dependence on newlines for parsing.  If it wasn't
necessary, then

 fun "foo" "bar" "baz"
 "quux":meth()

would have to parse differently from either

 fun "foo" "bar" "baz" "quux":meth()

or

 fun "foo" "bar" "baz";
 "quux":meth()

By the way, if you've never seen the 'fun "foo" "bar" "baz"'
syntax before, here's an example of it:

 > function fun(s)
 >>   print(s)
 >>   return fun
 >> end
 > fun "foo" "bar" "baz"
 foo
 bar
 baz

--
Aaron