[英]How can I access a method which return Option object?
I have to call method which returns Option[List[Obj]]
.我必须调用返回
Option[List[Obj]]
。
After I call I need to iterate the List and print the Obj attributes.调用后,我需要迭代 List 并打印 Obj 属性。
object Tester{
def main(args:Array[String]) {
val ymlFilename ="some.yml";
val entities: Option[QueryEntities] = InputYamlProcessor.process(ymlFilename)
for( e: QueryEntities <- entities ){
/// this is not working
//How to access the columnFamily, fromData and toDate ?
}
}
Complete sample完整样品
https://gist.github.com/shatestest/fdeaba767d78e171bb6c08b359fbd1bf https://gist.github.com/shatestest/fdeaba767d78e171bb6c08b359fbd1bf
The most iconic way ot do it is to unwrap values with scala is to use pattern matching to unwrap the value.最有代表性的方法是使用 Scala 解包值,即使用模式匹配来解包值。
entities match {
case Some(queryEntities: QueryEntities) =>
queryEntities.entities.foreach { case e =>
println(e.columnFamily)
println(e.fromDate.getOrElse("defaultFromDateHere")
println(e.toDate.getOrElse("defaultToDateHere"))
}
case None => println("No value")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.