scala 值 toInt 不是 Any 的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17673958/
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 05:30:21  来源:igfitidea点击:

scala value toInt is not a member of Any

scalatypescastingoption

提问by user644745

The println in the following code works (with or without toInt)

以下代码中的 println 有效(有或没有toInt

println("retweets : ", e.getOrElse("retweets", 0).toInt)

top10Tweets(""+e.get("text").get, e.getOrElse("retweets", 0).toInt)

But when I pass it as an argument of a function (as above), it does not work. It says "value toInt is not a member of Any"

但是当我将它作为函数的参数传递时(如上),它不起作用。它说“值 toInt 不是 Any 的成员”

When I remove toInt, it says,

当我删除 toInt 时,它说,

    type mismatch;
[error]  found   : Any
[error]  required: Int

e is a Map, as follows,

e是一个Map,如下

  def tweetDetails(obj: twitter4j.Status) = {
   Map(
   "id" -> obj.getUser().getId(),
   "screenName" -> obj.getUser().getScreenName(),
   "text" -> obj.getText(),
   "retweets" -> obj.getRetweetCount(),
   "mentions" -> obj.getUserMentionEntities().length)
  }

signature of top10Tweets,

前10条推文的签名,

def top10Tweets(tweets: String, retweet_c: Int, mention_c: Int) = {
}

回答by drexin

edit:

编辑

Ok, with the new information I would suggest you to create a case class that holds the data instead of using a Map, this way you will preserve type information. I know it is common to use hashes/maps for that in dynamically typed languages, but in statically typed languages as scala data types are the preferred way.

好的,有了新信息,我建议您创建一个保存数据的案例类,而不是使用 a Map,这样您就可以保留类型信息。我知道在动态类型语言中使用哈希/映射是很常见的,但在静态类型语言中,scala 数据类型是首选方式。

orig:

来源

As I neither know what eis, nor what signature top10Tweetshas, I can only assume. But from your code and the error I assume that eis a Map[String, String]and you are trying to get the string representation of an integer for the key "retweets"and convert it to an Int. As a default value you pass in an Int, so the type inferencer infers type Any, because that is the most common super type of Stringand Int. However Anydoes not have a toIntmethod and thus you get the error.

因为我既不知道是什么e,也不知道签名top10Tweets有什么,我只能假设。但是根据您的代码和错误,我假设e是 aMap[String, String]并且您正在尝试获取键的整数的字符串表示形式"retweets"并将其转换为Int. 作为默认值传入 an Int,因此类型推断器会推断 type Any,因为这是Stringand最常见的超类型Int。但是Any没有toInt方法,因此您会收到错误消息。

Map("x" -> "2").getOrElse("x", 4).toInt
<console>:8: error: value toInt is not a member of Any
              Map("x" -> "2").getOrElse("x", 4).toInt

Either pass in the default value as String, or convert the value of "retweets"to an Intbefore, if it exists:

无论是传递默认值String,或值转换"retweets"Int之前,如果存在的话:

e.get("retweets").map(_.toInt).getOrElse(0)

Anyway a little more information would help to give an accurate answer.

无论如何,多一点信息将有助于给出准确的答案。

回答by daaatz

Yes because in Map is "string" -> "string" and You did when getOrElse ( else ) string -> int, thats why its Any.

是的,因为在 Map 中是“字符串”->“字符串”,而当 getOrElse(else)字符串-> int 时,您这样做了,这就是为什么它的 Any。

Map("x" -> 2).getOrElse("x", 4).toInt

works fine or You can:

工作正常,或者您可以:

Map("x" -> "2").getOrElse("x", "4").toInt

回答by Y.fei

The same problem bothers me before you could check below

在你查看下面之前,同样的问题困扰着我

Map("x" -> 2).getOrElse[Int](x, 4)