From 15553ca3ff3dc762452808e6b46c667ae047209a Mon Sep 17 00:00:00 2001 From: "ajeffrey@roblox.com" Date: Wed, 15 Mar 2023 18:06:10 -0500 Subject: [PATCH] Add a drawback about multi-constructor classes --- rfcs/shared-self-types.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rfcs/shared-self-types.md b/rfcs/shared-self-types.md index 6c27c9d9..6e8a6b7a 100644 --- a/rfcs/shared-self-types.md +++ b/rfcs/shared-self-types.md @@ -343,6 +343,19 @@ This is a similar problem, caused by calling methods directly on metatables as well as tables. For example calling `Point:abs()` will result in inferring that both `x` and `y` are optional, +### Classes with multiple constructors + +With the current greedy unifier, classes with constructors of different types will fail: + +```lua + local Foo = {} + Foo.__index = Foo + function Foo.from(x) return setmetatable({ msg = tostring(x) }, Foo) end + function Foo.new() return setmetatable({}, Foo) end +``` + +rather than inferring an optional field, this will report a type error. + ## Alternatives We could use new syntax for declaring self types, rather than using