icerockdev / moko-kswift

Swift-friendly api generator for Kotlin/Native frameworks
https://moko.icerock.dev
Apache License 2.0
351 stars 21 forks source link

SealedToSwiftEnumFeature: Any is not a class type #11

Closed mattmook closed 3 years ago

mattmook commented 3 years ago

Consider the following sealed class.

sealed class Status<T : Any> {
    data class Success<T : Any>(val data: T) : Status<T>()
    data class Failure<T : Any>(val exception: Exception) : Status<T>()
}

SealedToSwiftEnumFeature converts this to:

public enum StatusKs<T : Any> {

  case success(StatusSuccess<T>)
  case failure(StatusFailure<T>)

  public init(_ obj: Status<T>) {
    if let obj = obj as? shared.StatusSuccess<T> {
      self = .success(obj)
    } else if let obj = obj as? shared.StatusFailure<T> {
      self = .failure(obj)
    } else {
      fatalError("StatusKs not syncronized with Status class")
    }
  }
}

However, as Any allows structs etc the code won't compile.

image

Instead, converting Any to AnyObject fixes the above compilation issue.

The Kotlin conversion of Status is as follows:

open class Status<T> : KotlinBase where T : AnyObject {

    public init()
}

public class StatusFailure<T> : Status<T> where T : AnyObject {

    public init(exception: KotlinException)

    open func component1() -> KotlinException

    open func doCopy(exception: KotlinException) -> StatusFailure<T>

    open func isEqual(_ other: Any?) -> Bool

    open func hash() -> UInt

    open func description() -> String

    open var exception: KotlinException { get }
}

public class StatusSuccess<T> : Status<T> where T : AnyObject {

    public init(data: T)

    open func component1() -> T

    open func doCopy(data: T) -> StatusSuccess<T>

    open func isEqual(_ other: Any?) -> Bool

    open func hash() -> UInt

    open func description() -> String

    open var data: T { get }
}
Alex009 commented 3 years ago

hi! thanks for feedback, we add this to tests and add support this case