I have `val items: Set<Any>` that contains d...
# announcements
j
I have
val items: Set<Any>
that contains different types including
EventItem
. I wanna find in my set the index of
EventItem
with the specific date like:
Copy code
val event = state.items
    .filterIsInstance(EventItem::class.java)
    .find { it.start == date.atStartOfDay() }
                
val index = state.items.indexOf(event)
However, it gives me
Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
What’s missing?
s
Seems
event
is of type
EventItem?
., a nullable type. This will have a successful type-inference:
val index = state.items.indexOf(event!!)
Or, the other way around, change the
items
type:
val items: Set<Any?>
s
Copy code
val index = state.items
      .filterIsInstance<EventItem>()
      .indexOfFirst { it.start == date.atStartOfDay() }
// index is potentially -1 (not found) in this implementation
h
@StavFX will this one returns the actual index? I guess we have to move the type check inside the indexOfFirst function.
s
Ah good point. Yeah, my implementation is shit 😅