我有這樣的對象:獲取選項根據對象的值
case class Student(name: String, dateTime: DateTime)
在這個函數中,我得到學生object
但DateTime
有時null
有時不是:
def foo(student: Student): Unit = {
}
這是新的功能得到Option[DateTime]
def foo2(option: Option[DateTime]): Unit = {
}
因此,當致電foo2
我需要先檢查我的DateTime
是否爲null
,然後致電foo2(None)
或DateTime
不是null
並致電Some(student)
。
所以我的問題是:如果不是使用此:
def foo(student: Student): Unit = {
if (student == null) foo2(None)
else foo2(Some(student))
}
任何想法如何確定student
值,並將其發送給foo2
功能,而無需使用if-else
?