This is a pre-RFC for inheritance.
Motivation
Use cases include GUIs and certain tree-like structures (ex. Servo’s DOM). In both cases, interfaces allow the type system to guarantee what it would otherwise not be able to. Servo is currently forced to use unsafe code to work around this.
Detailed design
Syntax
A class is declared as follows:
struct class X {
...
}
// OR
enum class X
...
}
// Here's how you extend another class
struct class Y : X {
...
}
// Same for enum-based classes
enum class Y : X {
...
}
The contextual keyword class
is added. It is valid only after the keyword struct
or enum
.
Only single inheritance is supported.
Methods of a class can be declared virtual by placing the virtual
contextual keyword before fn
. To override a method, the override
contextual keyword must be used in the same place.
Typing
Classes do not implement Sized
. Therefore, only references can be used. A subclass reference can be cast to a superclass reference using the upcast
method or the (new) :>
operator.
In general, type inference in the presence of subtyping is difficult, so upcasting is not implicit.