lua-users home
lua-l archive

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


A pattern in Lua's history is that some features appear first as a
special-case feature that solves a narrow set of problems, and are
later generalized into a broader mechanism.

Lua first had fallbacks, then those became tag methods, which
eventually became metatables.

Lua first had %var upvalues, then eventually gained lexical scoping.

If the same happens to the <toclose> and <const> constructs, the
obvious direction for them as a broad mechanism is to become
decorators/annotations/attributes in the future.

* Python decorators: https://wiki.python.org/moin/PythonDecorators
* Java annotations: https://en.wikipedia.org/wiki/Java_annotation
* C# attributes:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/
* Swift annotations:
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html

(funny that there doesn't seem to be a standard name for this, but
perhaps the different names are there to signal the variations in
semantics. I'll call them annotations as it wins my sampling by a
narrow margin.)

Python, Java and Swift use the @annotation syntax; C# uses the
[annotation] syntax.

Given that a future generalization of annotations would probably mean
that the annotation name would be user-extensible as any identifier,
Lua 5.4's current choice of syntax means that an eventual
generalization would add `'<' Ident '>'` to the grammar. Given that
such generalization as seen in other languages might include adding
annotations to function declarations, we would end up with `'<' Ident
'>' 'function' Name '(' Args ')'`.

Taking a page from past struggles of the C++ grammar, I'm concerned
that using '<' like that would introduce ambiguites that would
preclude general function annotations in the commonly-adopted
annotation style. The following would be hard to parse without
significant lookahead:

   local x = y

   <my_custom_annotation(with, arguments)>
   function my_global_function()
      -- ...
   end

If the door is to be left open for such a generalized mechanism in
future versions of Lua beyond 5.4, then adopting a syntax based on an
unused token such as @annotation would be advisable. Matching the
annotation syntax of other languages would be a side benefit.

-- Hisham