简体   繁体   中英

Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types

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. 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. ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM