lua-users home
lua-l archive

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


Actually, parentheses are required around any type of literal, not
just a string literal, in order to call a method on it or to index it:

> ({ 'a', 'b', 'c', concat = table.concat }):concat("|")
a|b|c
> { 'a', 'b', 'c', concat = table.concat }:concat(", ")
stdin:1: unexpected symbol near '{'
> ({ 'a', 'b', 'c', concat = table.concat }).concat
function: 0xXXXXXXXXXXXX
> { 'a', 'b', 'c', concat = table.concat }.concat
stdin:1: unexpected symbol near '{'

> debug.setmetatable(print, { __index = { curry = function (self, arg) return function (...) return self(arg, ...) end end } })
> (function (a, b) return a + b end):curry(1)(2)
3
> function (a, b) return a + b end:curry(1)(2)
stdin:1: <name> expected near '('
> (function (a, b) return a + b end).curry
function: 0xXXXXXXXXXXXX
> function (a, b) return a + b end.curry
stdin:1: <name> expected near '('

> debug.setmetatable(2, { __index = math })
> (2):pow(3)
8.0
> 2:pow(3)
stdin:1: unexpected symbol near '2'
> (2).pow
function: 0xXXXXXXXXXXXX
> 2.pow
stdin:1: unexpected symbol near '2.'

If this restriction were removed from string literals, it would make
sense to remove it from table, number, and function literals as well,
though only string and table literals would be likely to be indexed
because indexing numbers and functions requires adding a metatable to
them with the debug library.

— Gabriel

— Gabriel



On Thu, Jan 24, 2019 at 8:32 AM Жаботинский Евгений
<evg-zhabotinsky@yandex.ru> wrote:
>
> "Soni "They/Them" L." <fakedme@gmail.com>:
> > Custom literals:
> >
> > local foo = u128"10000000000000000000000000000000000000"
> > local bar = re"[A-Za-z_][A-Za-z0-9_]*"
>
> Hm, yes. That is useful.
> Also, in these examples the function call actually looks like a literal prefix.
> If such convention is followed, it really is obvious what the code does.
>
> Fun fact:
>
> > "%02X":format(42)
> > -- Error
> > function s(v) return v end
> > s"%02X":format(42)
> > -- "2A"
>
> So… I guess it does not make much sense to force use of parentheses for method calls only around "primitive" string literals.
>
> -- Evgeniy Zhabotinskiy
>