What is opt keyword in following code?
import scala.util.control.Exception._
import java.net._
val s = "http://www.scala-lang.org/"
val x1 = catching(classOf[MalformedURLException]) opt new URL(s)
val x2 = catching(classOf[MalformedURLException], classOf[NullPointerException]) either new URL(s)
http://www.scala-lang.org/api/current/scala/util/control/Exception $.html
It's not a keyword, it's a method, defined in Catch[T]
, which the result type of catching()
. See its doc at http://www.scala-lang.org/api/current/index.html#scala.util.control.Exception$$Catch@opt[U%3E:T](body:=%3EU):Option[U]
The above code is equivalent to the following, where we use .
syntax to call the methods opt
and either
:
val s = "http://www.scala-lang.org/"
val x1 = catching(classOf[MalformedURLException]).opt(new URL(s))
val x2 = catching(classOf[MalformedURLException], classOf[NullPointerException]).either(new URL(s))
It's not a keyword , it's a method imported from scala.util.control.Exception._
, here's its definition and Scaladoc:
/** Apply this catch logic to the supplied body, mapping the result
* into `Option[T]` - `None` if any exception was caught, `Some(T)` otherwise.
*/
def opt[U >: T](body: => U): Option[U] = toOption(Some(body))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.