Inheritance
Shift supports single inherietence from any type of the same kind. There are some restrictions which kinds can inherit from what.
- Structure
- Structures
- Records
- All inherited fields will be readonly
- Interfaces
- Record
- Interfaces
- Records
- Library
- Nothing - As a consequence of a library actually being a value and not a type.
- Service
- Interfaces
- Services
- Records
- Interface
- Interfaces
- Enum
- Nothing
- Though a type alias to a enum union would let you combine the list of values together.
Furthermore a type can subtype any number of interfaces. This does require the interface is fully implemented. That means any fields from the interface must match any declaration rules for the type's kind.
For example
interface SomeFields
{
readonly int A;//Fields take the default implementation
}
record MyRecord : SomeFields
{
string SomeTextData;
}It is ok for MyRecord to subtype SomeFields since field A is readonly and a record. This means any method taking SomeFields in will be constrained in its interface with field A the same way a record would be. If it wasn't marked readonly or if A's type wasn't a record then the interface couldn't be implemented, and this wouldn't compile.
Subtypes
When a type inherits or implements an interface it becomes a subtype of that. Anywhere that constrains to a specific type, a subtype can alternatively be supplied. Which is to say type satisfaction for type safety is covariant.
interface A
{
string Name;
string ToString();
}
structure Foo : A
{
string ToString() => $"Honk Honk I am {Name}";
}
structure Bar : Foo
{
this()
{
Name = "Biggest Honk"
}
}The subtype graph for this would look like
graph TD;
Bar-->Foo;
Foo-->A;- Foo IS a subtype of A. So anywhere requiring an A, a Foo can be supplied.
- Bar IS a subtype of Foo and by extension is also a subtype of everything Foo is.
If you had a method or a variable requiring an A you could pass either a Foo or Bar variable in.
var ex = new Foo();
var ex2 = new Bar();
A ex3 = ex;
A ex4;//Throws an exception, interfaces cannot be instantiated
Console.WriteLine(ex.ToString());// "Honk Honk I am "
Console.WriteLien(ex2.ToString());// "Honk Honk I am Biggest Honk"