lua-users home
lua-l archive

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


The ":" is a binary operator used in expressions, it is bound to a specific function that creates a binding between to object, that binding being a new object that can be used to perform a function call in a special way (which modifies the list of operands by prepending the first parameter of the binding). The ":" can be used between any pair of objects.

Syntactically "a:integer" is just equivalent to a (meta-)function call like "__bind(a, integer)", just like the syntax using "." or "[]" binary operators, which are also equivalent to a (meta-)function calls. And note that "a" and "integer" are just regular identifiers for objects/variables of any type.

Given the extended scope of usage of ":", ".", and "[]", they cannot be reused to mark a new kind of operation or annotation as this will necessarily be ambiguous.

We can reuse ":" or "." ONLY if it does not follow what could be an _expression_, for example immediately after some specific keywords (for example simple statement keywords like "local" or "return", or keywords starting compound statement constructs like "for", "while", "if", "else", "begin",  "function") or in specific places inside expressions where it cannot have its existing role of binary operator (for example immediately after "(", or after "=" inside assignment/"for" statements).

The same applies to "<" and "*" that have also been proposed to create such annotations: such reuse however limits severely the possibility of inserting annotations within expressions (only possibility left: surround the annotation starting by these "<" or "*" or ":" or "." by placing the whole annotation in parentheses)

So if you want to reuse the ":" for annotations, then it should be unambiguously:
  local x = (:integer)a + (:integer)b
and certainly not:
  local x = a:integer + b:integer

If you want to reuse the "<" or "*" that have also been proposed, it should be:
  local (*toclose) a = _expression_
  local (<toclose>) a = _expression_
and not:
  local *toclose a = _expression_
  local <toclose> a = _expression_
so that it can be generalized for use even outside the "local" declaration, in statement expressions notably:
  (*toclose) a = _expression_
  (<toclose>) a = _expression_
Finaly the proposed syntax using the pair "<" ... ">" is the worst solution.

Why this nightmare ? Can't we just use "@" and completely avoid the parentheses and all ambiguities at the same time, while preserving also the simplicity and efficiency of the syntax parser (no need to implement the complexity of rollbacks, with their additional cost in memory, that are needed to conditionnally resolve the shift/reduce conflicts by two successive retries) ?


Le ven. 7 juin 2019 à 08:20, Egor Skriptunoff <egor.skriptunoff@gmail.com> a écrit :
On Wed, Jun 5, 2019 at 6:17 PM Xavier Wang wrote:
So, we just do not append a '(' after the name, like this:
local x = a:integer + b:integer
It's invalid in current Lua.
or like this:
(a:integer[])[10]
for multiple annotations, this should works:
local x:toclose:MyClass = MyClass.new()



How the following should be interpreted:
   local x = a:MyClass(b:MyAnotherClass)
Does it mean
   local x = a.MyClass(a, b)
or
   local x = a(b)
?