Interfaces and variants
Interfaces
An interface type is like a union, but does not declare its case types.
Instead, each case type declares itself a case of the interface.
Strong interfaces
There's no way to retrieve the case type given a reference to an interface.
The only thing you can do with an interface value is call its methods.
This is usually a good thing as it makes an interface a meaningful specification
of what a value is actually used for.
If you want to match on an interface, use a variant instead.
Interface mutability
As with a union, case types of an interface can have at most the interface's mutability.
Variants
variants are like interfaces, but support match expressions.
Or to put it another way, a variant is like a union
where the case types do not have to be known up-front.
These aren't often needed, but the exception type is an example variant type.
It's a good example where it would be impractical to declare all case types up-front.
Restrictions
- Since a case type must explicitly
casean interface or variant, they can't contain already-declared types likenat. - Auto functions won't work on interfaces or variants.