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

David Glasser

03/29/2023, 12:09 AM
I'm having trouble using minByOrNull within buildMap, getting odd type mismatch errors about TypeVariables. Am I missing something? https://pl.kotl.in/GnuWqAS_Z
(I'm trying to make a Map out of another Map by first filtering it based on one condition, and then removing the smallest element based on a second condition. I figure buildMap is a nice way to not have to have an explicit MutableMap in the middle. But minByOrNull doesn't work on the
this
of the buildMap closure for some reason...)
OK I guess it's a type inference thing; specifying
<String, F>
myself on
buildMap
(or its return value) works
a

asdf asdf

03/29/2023, 1:22 AM
You could also simplify
output
into something like this for more readability / avoiding type inference oddities:
d

David Glasser

03/29/2023, 4:15 AM
@asdf asdf thanks, but in the real version I'm doing a
remove
on the output of the minByOrNull 🙂
s

Stephan Schröder

03/29/2023, 8:10 AM
your code also works if you add type annotations to buildMap
Copy code
val output = buildMap<String, F> {
looks like you reached the limits of type inference
i

ilya.gorbunov

04/05/2023, 6:52 PM
d

David Glasser

04/06/2023, 5:54 AM
thanks!
4 Views