Composite Type Subtyping
// Declare a structure named `A` which has a function `test`
// which has type `((): Void)`.
//
struct A {
fun test() {}
}
// Declare a structure named `B` which has a function `test`
// which has type `((): Void)`.
//
struct B {
fun test() {}
}
// Declare a variable named which accepts values of type `A`.
//
var something: A = A()
// Invalid: Assign a value of type `B` to the variable.
// Even though types `A` and `B` have the same declarations,
// a function with the same name and type, the types' names differ,
// so they are not compatible.
//
something = B()
// Valid: Reassign a new value of type `A`.
//
something = A()PreviousAccessing Fields and Functions of Composite Types Using Optional ChainingNextComposite Type Functions
Last updated