From ba052cea8e4aeab45692e588302028ef04d9789a Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 19 Nov 2023 14:08:23 -0300 Subject: [PATCH] add comment explaining "." vs. ":" handling From: https://github.com/teal-language/tl/issues/692 > It took me a bit of digging the back history to try to remember why `.` and > `:` have separate handlers. Of course, in Lua, `:` is just syntactic sugar. In > Teal, however, we have in the language semantics separate concepts for > functions and record-functions/methods, because the latter are statically > bound to their records. > > For example, want to be able to detect that the programmer made a typo and a > method name is invalid, and that means that we need, at one point of the code, > to declare that the declarations of methods are done. So you can only do > `function my_record:mymethod` in the same scope where you created your record, > for instance. > > Internally, Teal does keep track if a function "is a method" (that is, if it > was declared with `:` or `.`; in more recent versions, we even check if a > function is method-like by having a first argument `self` with its own type). > This is to keep the door open for future enhancements to the type system, such > as interfaces (see this comment: > https://github.com/teal-language/tl/issues/97#issuecomment-623795757). > > So, it's an explicit decision to not make `:` just a syntactic sugar for `.`, > like Lua. --- tl.tl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tl.tl b/tl.tl index 421a6b702..263e43683 100644 --- a/tl.tl +++ b/tl.tl @@ -10004,6 +10004,8 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string end node.type = BOOLEAN elseif node.op.op == ":" then + -- we handle ':' separately from '.' because ':' is specific to records, + -- so we produce different error messages if lax and (is_unknown(a) or a.typename == "typevar") then if node.e1.kind == "variable" then add_unknown_dot(node.e1, node.e1.tk .. "." .. node.e2.tk)