RFC: Class Constructor Modifier - #240
Conversation
|
I've got a couple questions for this RFC:
|
If There was already a lot of pushback against the If we're going to use function-call syntax anyway, I think something more like the But if we want to preserve conventions like
If constructors are implemented as static functions like they are now, inheritance becomes difficult to design around. class Car
public IsDestroyed: boolean
public Speed: number
function new(speed: number)
return Car { IsDestroyed = false, Speed = speed }
end
end
class ElectricCar extends Car
public Capacity: number
function new(capacity: number)
const self = Car.new(math.max(50 - capacity * 2, 0))
-- How do we know whether this Car is intended to become an ElectricCar,
-- or whether it should remain just a Car?
-- We'd have no choice but to report an error here.
self.Capacity = capacity
return self
end
endA normal static function has no way to express that That's why I think constructors need semantics beyond those of an ordinary static factory method. |
Standard conventions should be optional instead of enforced, and I believe that makes the function-call syntax superior to using/reserving a If |
Rendered
This RFC introduces a constructor modifier for declaring a class's constructor while allowing its name to follow existing Luau conventions.
Classes should not become difficult to understand or use, and I am concerned that the current direction may be making them unexpectedly more complex.
Even if this proposal is not accepted, I still hope classes will move in a direction that keeps them simple and intuitive.