Cicero
12/01/2020, 11:30 PMprivate var filteredList = mutableListOf<Any>()
if(filteredList.contains(item.resourceType)
data class Item (
val resourceType: ResourceType? = null
)
Now if I give it the ResourceType straight it works. But I would like to understand why not.
PS: The way I’m going around this, for now, I’m surrounding the case with when(type) and giving the type class directly into it ex:
if(filteredList.contains(ResourceType)Chris Ruddell
12/02/2020, 12:12 PMresourceType if it’s just a list of Any objects, since Any doesn’t have that property. If your goal is to filter filteredList again to only include items of type Item, you could do something like this:
val filteredItems = filteredList.filterIsInstance<Item>()
That will produce a new list (filteredItems) which the compiler knows only contains Item objects. Then you can reference your property.
Alternatively, you could do something like this, although its a bit more verbose:
filteredList.forEach { listItem ->
if (listItem is Item) {
// now the compiler knows listItem is an Item and you can reference its properties
} else {
// the compiler still thinks listItem is an Any object here
}
}Cicero
12/02/2020, 2:22 PMChris Ruddell
12/02/2020, 2:39 PM