0

I am trying to create a POJO (aka data classes in Kotlin) structure of a JSON response in Kotlin. I've implemented the Parcelable interface for each data class in the structure. In all of the data classes, I've auto generated the Parcelable implementation. The issue is the generated second constructor where the IDE is complaining about:

Overload resolution ambiguity

It states that it's being confused between these two constructors:

public constructor GeocodeRes(parcel: Parcel)
public constructor GeocodeRes(responset: ResponseRes)

Which I believe makes sense because ResponseRes is also of type Parcelable (ResponseRes implements Parcelable). So calling the GeocodeRes(parcel) method (within the createFromParcel companion method), it is getting confused.

That was until I removed ResponseRes from implementing the Parcelable class and it's still showing the same error.

Is there any reason to this? Am I setting this up properly? In all of the children data classes, they all implement the Parcelable interface (with dependence with eachother) but aren't running into any issues.

Here's my GeocodeRes class:

import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

data class GeocodeRes(
@SerializedName("Response") @Expose val responset: ResponseRes
) : Parcelable {



// this is the problem. the IDE is complaining that the usage is too ambiguous (). however, the only usage of this constructor is within this class - just doesn't tell me where exactly. 
constructor(parcel: Parcel) : this(parcel.readParcelable(ResponseRes::class.java.classLoader)) {
}



override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeParcelable(responset, flags)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<GeocodeRes> {
    override fun createFromParcel(parcel: Parcel): GeocodeRes {
        return GeocodeRes(parcel)
    }

    override fun newArray(size: Int): Array<GeocodeRes?> {
        return arrayOfNulls(size)
    }
}


}

Here's my ResponseRes class:

data class ResponseRes(
    @SerializedName("MetaInfo") @Expose val metaInfo: MetaInfo,
    @SerializedName("View") @Expose val views: List<View>
    ): Parcelable 
{
[...]//parcel methods
}
rj2700
  • 1,770
  • 6
  • 28
  • 55

1 Answers1

1

however, the only usage of this constructor is within this class - just doesn't tell me where exactly

The problem is with the definition itself, not with any usage. It could never be used, and the error would still be there.

You should be able to fix this by specifying which Parcelable you want to read:

this(parcel.readParcelable<ResponseRes>(ResponseRes::class.java.classLoader))

The compiler can't decide if you mean that or

this(parcel.readParcelable<Parcel>(ResponseRes::class.java.classLoader))

Even though the second wouldn't be legal because Parcel doesn't implement Parcelable, if you look at the signature

<T extends Parcelable> T readParcelable(ClassLoader loader) 

you can see only the return type can be used to infer T, not the argument. So the compiler need to pick the constructor overload before trying to infer T.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487