0

I have a class like follow:

class Weekend<T : Any> {
   
   fun <R> equal(property: KProperty1<T, R>, value: R?) {
      ...
      return this
   }
}

and use the class like follow:

Weekend<User>().equal(User::name, "username")

in this case, User.name is a string property, and "username" is also a string. But if I write the code like Weekend<User>().equal(User::name, 22), kotlin will not give errors, because string and Int has the same parent Compareble.

Is there any way to tell kotlin the second param value must be a string here? (I know Weekend<User>().equal<String>(User::name, 22) will work, but I want a simple way to do this.)

  • The answer @Sweeper points to provides the answer as to why this is currently not possible, in fact the issue tracker uses an almost identical example. – Laurence Dec 13 '21 at 08:38

1 Answers1

0

If the second param must be a string just set it as String?

fun equal(property: KProperty1<T, String>, value: String?)
Simone
  • 1,418
  • 15
  • 22