Home > Back-end >  Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types
Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types

Time:07-07

I'm new to Kotlin, for the piece of the below code:

fun a(stcd: String) {
        val res = mutableSetOf<String>()
        val aaa = mutableListOf<Map<String, Set<String>>>()
        aaa.stream().filter { x: Map<String, Set<String>> -> x.isNotEmpty() }
            .filter { x: Map<String, Set<String>> ->
                x.values.contains(stcd) // throws error
            }.forEach { x: Map<String, Set<String>> ->
                x.forEach { (k: String, v: Set<String>?) ->
                    res.add(k)
                }
            }
    }

Could anyone point out why contains throws error: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.?

CodePudding user response:

This is because x.values is not a Set<String>, as you probably think it is. In fact, it's aCollection<Set<String>> as per the definition of values. So, such a collection can't contain a String type.

  • Related