Skip to content

Commit

Permalink
add comment explaining "." vs. ":" handling
Browse files Browse the repository at this point in the history
From: #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:
> #97 (comment)).
>
> So, it's an explicit decision to not make `:` just a syntactic sugar for `.`,
> like Lua.
  • Loading branch information
hishamhm committed Nov 20, 2023
1 parent cecb87d commit ba052ce
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ba052ce

Please sign in to comment.