Skip to content

Latest commit

 

History

History
57 lines (39 loc) · 1.68 KB

File metadata and controls

57 lines (39 loc) · 1.68 KB

Enum classes

No equivalent enum is generated on the Swift side, and default case must be specified in a switch expression. Instead an object with static elements is generated. Improved interop available with SKIE.

Explanations

Let's describe a regular enum class in Kotlin:

enum class EnumClass(val type: String) {

    ENTRY_ONE("entry_one"),
    ENTRY_TWO("entry_two");

    companion object {
        fun findByType(type: String) = values().find { it.type == type }
    }

}

No enum is generated on the Swift side, and as a result, the class cannot be conveniently used in a switch expression and therefore it is necessary to specify a default case.

But there is access to the elements of the enum, there is access to the function inside the companion object, it is possible to call the values() function.

func useEnumClass() {
    let e1 = EnumClass.entryOne
    let _ = EnumClass.entryTwo
    
    let _ = EnumClass.entryOne.name
    let _ = EnumClass.entryOne.type
    
    let _ = EnumClass.companion.findByType(type: "entry_two")
    let _ = EnumClass.companion.findByType(type: "entry_two_trheee")
    
    switchEnumClass(enumClassExample: e1)
}

private func switchEnumClass(enumClassExample: EnumClass) {
    switch enumClassExample {
        case .entryOne: print("entryOne")
        case .entryTwo: print("entryTwo")
        default:
            print("default")
    }
}

Kotlin/Native does not yet support enum generation.

SKIE

SKIE creates real Swift enums, and automatically converts them from Kotlin enums. Enums are exhaustive, so the default is no longer required.


Table of contents