Hi :wave: I'd like to declare a data class propert...
# announcements
t
Hi 👋 I'd like to declare a data class property as a function:
matcher: (x: Any) -> Boolean
. Later when i pass in a lambda for matcher, I have to cast from Any to the concrete type as per blew. Is there a better way to do this?
Copy code
data class Query(
 matcher: (x: Any) -> Boolean
)
...
val query = Query(
 matcher: { x: Any? ->
   val value = x as Int?
   if(value == null || value == 0) false else true
 }
)
m
Use generics:
Copy code
class Query<T>(
 val matcher: (x: T) -> Boolean
)

val query = Query { value: Int ->
   if (value == 0) false else true
}

fun main() {
  println(query.matcher(0))
  println(query.matcher(1))
}
t
perfect thank you 🙏
k
You should probably use
class Query<in T>
👍 2