Object is not a value error in scala

后端 未结 4 750
礼貌的吻别
礼貌的吻别 2020-12-18 21:14

While trying to make a map in Scala, I receive the following error message: object Map is not a value

The code I\'m using is the following:

         


        
4条回答
  •  死守一世寂寞
    2020-12-18 21:42

    When you see the error "object is not a value" this typically means that the in-scope type is a Java type - you probably are importing java.util.Map in scope

    scala> Map(1 -> "one")
    res0: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> one)
    

    But

    scala> import java.util.Map
    import java.util.Map
    
    scala> Map(1 -> "one")
    :9: error: object Map is not a value
                  Map(1 -> "one")
                  ^
    

    Remember, in scala each class comes with a (optional) companion object which is a value. This is not true of Java classes.

提交回复
热议问题