Skip to content

Latest commit

 

History

History
68 lines (53 loc) · 2.2 KB

Sealed interfaces.md

File metadata and controls

68 lines (53 loc) · 2.2 KB

Sealed interfaces

Separate protocols are generated that are not related to each other.

Explanations

Let's describe the sealed interface on the Kotlin side; in the when expression there is autocompletion with consideration of all options:

sealed interface SealedInterfaces {

    interface First : SealedInterfaces {
        fun firstFunctionExample(): String
    }

    interface Second : SealedInterfaces {
        fun secondFunctionExample(): String
    }
}

fun sealedInterfaceExample(sie: SealedInterfaces) {
    when (sie) {
        is SealedInterfaces.First -> {
            sie.firstFunctionExample()
        }
        is SealedInterfaces.Second -> {
            sie.secondFunctionExample()
        }
    }
}

On the Swift side, separate protocols were generated that are not inherited from each other, this can be seen from the .h Objective-C file:

__attribute__((swift_name("SealedInterfaces")))
@protocol SharedSealedInterfaces
@required
@end

__attribute__((swift_name("SealedInterfacesFirst")))
@protocol SharedSealedInterfacesFirst <SharedSealedInterfaces>
@required
- (NSString *)firstFunctionExample __attribute__((swift_name("firstFunctionExample()")));
@end

__attribute__((swift_name("SealedInterfacesSecond")))
@protocol SharedSealedInterfacesSecond <SharedSealedInterfaces>
@required
- (NSString *)secondFunctionExample __attribute__((swift_name("secondFunctionExample()")));
@end

Accordingly, when substituting into a switch Xcode does not offer to consider all branch options.

func switchOnSealedInterfaces(sealedInterfaces: SealedInterfaces){
    switch(sealedInterfaces){
    case is SealedInterfacesFirst: print((sealedInterfaces as! any SealedInterfacesFirst as SealedInterfacesFirst).firstFunctionExample())
    case is SealedInterfacesSecond: print((sealedInterfaces as! any SealedInterfacesSecond as SealedInterfacesSecond).secondFunctionExample())
    default: print("default")
}

In Swift, the role of sealed interfaces is played by enums, and it would be possible to write special bridge code to convert a sealed interface into a Swift enum. See a possible approach.


Table of contents