teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Record function declaration fails when function type is in an union type #685

Closed 13k closed 8 months ago

13k commented 11 months ago

When a record has a field with a union type that could be a function, declaring the field as that function fails.

Test case:

local record R
  a: boolean | function(string, integer): string
end

local r: R = {}

-- assignment works as expected

r.a = function(s: string, i: integer): string
  return ""
end

-- declaration fails

function r.a(s: string, i: integer): string
  return ""
end
type signature of 'a' does not match its declaration in R: got function(string, integer): string, expected boolean | function(string, integer): string
hishamhm commented 8 months ago

Currently, declarations of record functions expect identical types if they are already declared in the record. As you realized, you can just use the assignment form for assignments that require subtyping relations. Honestly, I actually like this behavior since this also helps the reader notice that there's something special going on with that record field.

13k commented 8 months ago

From an OO perspective, it feels like assignments are "you can replace this object's attribute at will" whereas declarations feels like "this is a behavior of the object".

When declaring records as classes, it feels right to use declarations to explicitly define behavior.

All of that is subjective, of course.

It's also probably an uncommon case where an object's field could be a function and something else.

Feel free to close as wontfix if you feel the current behavior is favored.

hishamhm commented 8 months ago

Thanks for the feedback! I'm closing this as the change is not currently planned, then.