scala 对象不是Scala中的值错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9079129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:50:43  来源:igfitidea点击:

Object is not a value error in scala

scalaobjectmap

提问by Alexandros

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

尝试在 Scala 中制作地图时,我收到以下错误消息:对象地图不是值

The code I'm using is the following:

我正在使用的代码如下:

val myStringMap =  Map[String, String]("first" -> "A", "second" -> "B", "third" -> "C")

I am quite puzzled as to why I cannot create this map because after looking at Scala documentation, it seems to me that the syntax of my code is proper.

我很困惑为什么我不能创建这个地图,因为在查看了 Scala 文档之后,在我看来我的代码的语法是正确的。

回答by oxbow_lakes

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.Mapin scope

当您看到错误“对象不是值”时,这通常意味着范围内类型是 Java 类型 - 您可能正在java.util.Map范围内导入

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")
<console>:9: error: object Map is not a value
              Map(1 -> "one")
              ^

Remember, in scala each classcomes with a (optional) companion objectwhich is a value. This is not true of Java classes.

请记住,在 Scala 中,每个都带有一个(可选的)伴随对象,它是一个值。这不适用于 Java 类。

回答by Clare

Just found this so maybe it will be useful to share my solution. If you have imported java.util.Map and need to use scala.collection.immutable.Map then use it with the full name so instead of

刚刚发现这个,所以分享我的解决方案也许会有用。如果你已经导入了 java.util.Map 并且需要使用 scala.collection.immutable.Map 然后使用它的全名而不是

 Map(1 -> "one")

do

scala.collection.immutable.Map(1 -> "one")

This way it will know what you mean

这样它就会知道你的意思

回答by B Custer

Because in scala each native scala class comes with an (optional) companion object (allowing for assignment from the companion object as in your example code) when incorporating a java class into scala code always remember to instantiate the class by invoking the constructor, ie. use keyword "new", thus creating a value.

因为在 scala 中,每个原生 scala 类都带有一个(可选)伴生对象(允许从伴生对象进行赋值,如示例代码中所示),所以在将 java 类合并到 Scala 代码中时,请始终记住通过调用构造函数来实例化该类,即。使用关键字“new”,从而创建一个值。

回答by Morteza Shahriari Nia

I got similar error on Lists. try this in scala console:

我在列表上遇到了类似的错误。在 Scala 控制台中试试这个:

import java.util.List object test { def a():List[String] = { val list = List[String](); null }}

import java.util.List object test { def a():List[String] = { val list = List[String](); null }}

you'll get the err "Object List is not a value."

你会犯错 "Object List is not a value."

You get it because you're hiding the built-in List type, that is because List is different from java.util.List

你得到它是因为你隐藏了内置的 List 类型,那是因为 List 与 java.util.List 不同

What if someone wants to use util.List?

如果有人想使用 util.List 怎么办?

you can use a qualified name or a rename import

您可以使用限定名称或重命名导入

! import java.util.{List => JList}

!导入 java.util.{List => JList}

import java.util.{List=>JList}

导入 java.util.{List=>JList}