scala.Some 不能转换为 java.lang.String

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

scala.Some cannot be cast to java.lang.String

scalacastingscalatra

提问by Alex Spangher

In this application, I'm getting this error:

在此应用程序中,我收到此错误:

scala.Some cannot be cast to java.lang.String

When trying this:

尝试此操作时:

x.email.asInstanceOf[String]

x.email is an Option[String]

x.email 是一个选项 [String]

Edit: I understand that I'm dealing with different types here, I was just wondering if there were a more concise way to do nothing with None then a

编辑:我知道我在这里处理不同的类型,我只是想知道是否有更简洁的方法可以对 None 然后 a

match { case....}

sequence. Because I am casting x.email into a String for JSON purposes, a null field will be handled by the JSON object, and I don't explicitly have to deal with it. Sorry for being unclear!!

顺序。因为我将 x.email 转换为用于 JSON 的字符串,因此 JSON 对象将处理空字段,而我不必明确处理它。不好意思说不清楚!!

回答by Richard Sitze

Well, it's clear to you from the errors and types that x.emailis not a String...

好吧,您很清楚错误和类型x.email不是String...

First, decide how you want to handle None(a valid option for something of type Option[String]). You then have a number of options, including but not limited to:

首先,决定您要如何处理None(类型为的有效选项Option[String])。然后,您有多种选择,包括但不限于:

x.email match {
case None => ...
case Some(value) => println(value) // value is of type String
}


Alternately, take a look at the getand getOrElsemethods on class Option.

或者,查看 上的getgetOrElse方法class Option

If you want to "degrade" the option to a String with a possible nullvalue, then use

如果要将选项“降级”为具有可能null值的字符串,请使用

x.email.orNull // calls getOrElse(null)


Finally, if you just don't care about the Nonecase (and want to ignore it), then just use a simple "for comprehension" which will "skip" the body in the Nonecase:

最后,如果您只是不关心None案例(并想忽略它),那么只需使用简单的“for comprehension”即可“跳过”None案例中的正文:

for (value <- x.email) {
  // value is of type String
}

回答by S.R.I

Casting isn't how you should be looking at conversions when it comes to Options. Have a look at the following REPL session:

当涉及到选项时,转换不是你应该如何看待转换的。看看下面的 REPL 会话:

C:\>scala -deprecation -unchecked 
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0).
Type in expressions to have them evaluated. Type :help for more information.

scala> val email:Option[String] = Some("[email protected]") 
email: Option[String] = Some([email protected])

scala> email.getOrElse("[email protected]") 
res0: String = [email protected]

scala>

You might also want to look at this SO question: What is the point of the class Option[T]?

您可能还想看看这个 SO 问题:Option[T] 类的意义何在?

and the Options API here

这里的选项 API

Generally speaking, casting/coercion are kind-of taboo in FP world. :)

一般来说,强制转换/强制在 FP 世界中是一种禁忌。:)

回答by mirandes

x.map(_.toString).getOrElse("")

回答by Jiri Kremser

You may want use pattern matching:

您可能需要使用模式匹配:

x.email match {
  case Some(email) => // do something with email
  case None => // did not get the email, deal with it
}