【发布时间】:2018-06-07 11:13:26
【问题描述】:
case class Person(@BsonProperty("first_name") firstName:Option[String],@BsonProperty("second_name") secondName:Option[String],@BsonProperty("person_age") var age:Int)
val me = Some(Person(Some(Ambareesh),Some(B),23))
Or
val me = None
Or
val me = Some(Person(Some(Ambareesh),None,23))
Or
val me = Some(Person(None,None,23))
someOperation(me.secondName / None) //How can I implement this behavior in single line.
someOperation(me.firstName / None)
def someOperation(name:Option[String]){
//Do ...
}
我对方法someOperation 的论点(Person 的实例)表示怀疑。参数本身是一个选项值,字段也是选项。如果其中任何一个是 None(argument or argument.fieldName) 我希望 None 作为返回,否则将字段值作为 Some(fieldValue)。有可能吗?
【问题讨论】:
-
对不起。我无法理解这个问题
-
val me = Some(Person(Some(Ambareesh),Some(B),23))。me.get.firstName.get应该返回None / firstName即 Option[String] 类似me.get.secondName.get。我有更多参数,例如 firstName 和 secondName,所以我应该能够在函数调用本身中引用它。 -
me.firstName不是已经是Option了吗? -
@Jatin 不,
me.firstName无法编译。 -
是的,如果我不是
None,me.get.firstName会起作用。