Cannot prove that Unit <:< (T, U)

后端 未结 3 742
感情败类
感情败类 2021-01-05 05:57

When trying to remove all Unit - () from a list, I tried to call toMap.

scala> List((), ()).filter(_ != ()).toMap


        
3条回答
  •  礼貌的吻别
    2021-01-05 06:03

    It means that the type of an element in the list can't be viewed as a tuple which is required to build a Map. A Map in a sense is a collection of tuples (and more).

    Illustration:

    scala> List(1).toMap
    :8: error: Cannot prove that Int <:< (T, U).
              List(1).toMap
                      ^
    scala> List(1 -> 2).toMap
    res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
    

    I can build a map from a list of tuples, but not from a list of single cardinality elements.

    Maybe you mean to say .map instead of .toMap? ;)

提交回复
热议问题