mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.83k stars 492 forks source link

Overload resolution ambiguity. All these functions match. #1030

Closed Airsaid closed 2 years ago

Airsaid commented 2 years ago

Hi, Mike. thanks a lot for this great library: amazing :-)

I am referring to the MultiTypeModelItemActivity example using several different item to show.

However, there is no inheritance between my multiple items, so when I use Any directly, I get the following problem:

Overload resolution ambiguity. All these functions match.

The example code:

val itemAdapter = ModelAdapter { element: Any ->
  if (element is Avatar) {
    AvatarItem(element)
  } else if (element is User) {
    UserItem(element)
  }
  throw IllegalArgumentException()
}

val fastAdapter = FastAdapter.with(listOf(itemAdapter))
val models = ArrayList<Any>()
for (i in 0 until Random.nextInt(0, 100)) {
  if (i % 3 == 0) {
    models.add(DataProvider.providerUser(i))
  } else {
    models.add(DataProvider.providerAvatar(i))
  }
}

itemAdapter.add(models) // failed: Overload resolution ambiguity. All these functions match.

Details

Checklist

mikepenz commented 2 years ago

Look a bit further in the error message. One part of the issue actually originates from the problem that your ModelAdapter does not have a proper generic type specification: ModelAdapter<Any, Nothing>

Screenshot 2022-03-25 at 09 26 58

The generic type should resolve to: <Model, Item : GenericItem> with GenericItem being a typealias for IItem<out RecyclerView.ViewHolder>

Screenshot 2022-03-25 at 09 28 23

What you want to do is to ensure the generic type for the adapter is properly defined.

The other issues results from the classes not having a common base interface or similar. In detail this also results from generics, but you can work around this easily by introducing a common interface. Somewhat like:

    { 
         val itemAdapter = ModelAdapter<X, GenericItem> { element: Any ->
            if (element is Avatar) {
                AvatarItem(element)
            } else if (element is User) {
                UserItem(element)
            }
            throw IllegalArgumentException()
        }

        val fastAdapter = FastAdapter.with(listOf(itemAdapter))
        val models = ArrayList<X>()
        for (i in 0 until nextInt(0, 100)) {
            if (i % 3 == 0) {
                models.add(User())
            } else {
                models.add(Avatar())
            }
        }

        itemAdapter.add(models)
    }

    interface X

    class Avatar(): X

    class User(): X

    class AvatarItem(val avatar: Avatar): AbstractItem<RecyclerView.ViewHolder>() {
        override val type: Int
            get() = TODO("Not yet implemented")
        override val layoutRes: Int
            get() = TODO("Not yet implemented")

        override fun getViewHolder(v: View): RecyclerView.ViewHolder {
            TODO("Not yet implemented")
        }
    }

    class UserItem(val user: User): AbstractItem<RecyclerView.ViewHolder>() {
        override val type: Int
            get() = TODO("Not yet implemented")
        override val layoutRes: Int
            get() = TODO("Not yet implemented")

        override fun getViewHolder(v: View): RecyclerView.ViewHolder {
            TODO("Not yet implemented")
        }
    }
Airsaid commented 2 years ago

I know the cause of the problem, there is duplication of method signatures because of generic erasure.

I wanted to express that when there is no association between two data classes, it is not possible to use entries of multiple types.

mikepenz commented 2 years ago

I am not sure if this can be resolved due to how generics work. If you have proposals I'd love to hear about them.

In any case as outlined above, you can always introduce an interface adding a common interface between your classes.

mikepenz commented 2 years ago

Closing due to inactivity