https://kotlinlang.org logo
#getting-started
Title
# getting-started
b

Bart Kleijngeld

02/09/2022, 3:40 PM
I'm getting a type error that I don't understand, and was hoping someone could help me with it.
Copy code
import java.io.File

private fun Format(filename: String): Format? {
    // Never mind the implementation here.
}

fun readFiles(files: Sequence<File>): Map<Format, List<Reader>> =
    files
        .groupBy { getFormat(it.toString()) }
        .filterKeys { it != null }
        .mapValues { it.value.map { f -> f.reader() } }  // ERROR: Type mismatch: inferred type is Format? but Format was expected
I would expect that
filterKeys
gets rid of all the entries with
null
valued keys, doesn't it? Is the issue perhaps that this is not known at compile time?
s

Sam

02/09/2022, 3:41 PM
👌
j

Joffrey

02/09/2022, 3:41 PM
Is the issue perhaps that this is not known at compile time?
This
b

Bart Kleijngeld

02/09/2022, 3:42 PM
Alright, thanks for confirming my suspicion. Fairly new to statically typed languages
j

Joffrey

02/09/2022, 3:42 PM
This is why on collections there are specialized methods like
filterNotNull
which do enforce the nullability in their return type, so it's known at compile time. I don't know if there are equivalents on maps though
b

Bart Kleijngeld

02/09/2022, 3:43 PM
Ah right. That's good to know. So I might need an entirely different approach here then
j

Jacob

02/09/2022, 4:10 PM
Copy code
files
    .mapNotNull { file ->
        getFormat(file.toString())?.let { it to file.reader() }
    }.groupBy { it.first }
b

Bart Kleijngeld

02/09/2022, 4:21 PM
Thanks @Jacob, that looks promising. It's just slightly off but I think I can get that to work. Sounds like less maintenance than my own
filterNotNullKeys
extension function on maps.
m

Michael de Kaste

02/09/2022, 4:24 PM
jacob was close enough, but needs the mapvalues mapped
Copy code
files
    .mapNotNull { file -> getFormat(file.toString())?.let { it to file.reader() } }
    .groupBy({ it.first }, { it.second })
j

Joffrey

02/09/2022, 4:24 PM
@Jacob The values in the grouping that you get from there will be the pairs, which is not ideal. I think you can use an overload of
groupBy
that also has a
valueTransform
so you get the correct values
👍 1
@Michael de Kaste I guess you were quicker 😄
b

Bart Kleijngeld

02/09/2022, 4:26 PM
Oh I wasn't aware you could map the values with a second lambda 🙂. Nice! Thanks guys
m

Michael de Kaste

02/09/2022, 4:27 PM
@Joffrey I am speed
blob thinking fast 3
e

ephemient

02/09/2022, 8:33 PM
you can also perform an cast; it can't be checked by the compiler, but as long as you're sure:
Copy code
@Suppress("UNCHECKED_CAST")
fun <K, V : Any> Map<K, V?>.filterNotNullValues(): Map<K, V> = filterValues { it != null } as Map<K, V>
alternately, you could create something like the stdlib's requireNoNulls() for maps
b

Bart Kleijngeld

02/10/2022, 6:39 AM
Thanks for that additional solution @ephemient. It's a little too advanced for my current level of Kotlin understanding though. Although I do think you might have misunderstood that I want to filter those that have non-null keys and not values, although that's only a minor change 😉.
3 Views