简体   繁体   中英

How to match nested option values in scala

 let x = new Row(job_id="hello", title=null)

 x match {
     case Row(
           job_id: String,
           title: Option[String]) => println("successful match")
     case _ => println("failed!")

}

For the code above when I try to match with an option type it actually matches with _ and gives me a warning shown below:

warning: non-variable type argument String in type pattern Option[String] is unchecked since it is eliminated by erasure

Basically the Row struct represents a sql row with nullable values and I want to be able to pattern match to it. Does anyone know how?

Just don't use type patterns ( : String and : Option[String] ), because null doesn't match them. Write

case Row(job_id, title) =>

and check inside.

(When you get a Row from Spark, it won't contain any Option[String] s anyway, just null or non-null String s(/ Int s/etc.).

You could also use custom extractor objects , eg

object OptString {
  def unapply(x: String) = Some(Option(x))
}
// repeat for other types

then

case Row(job_id, OptString(title)) =>

will bind title to None for your x and wouldn't match for

new Row("hello", notAString) 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM