scala:如何返回空对象而不是None

时间:2015-05-12 17:21:41

标签: scala

val customerIdent = custIdentByCustomerId(id).firstOption
customerIdent.getOrElse (None)

我不想返回None,想要返回类型为CustomerIdentification的空customerIdent。

如果我这样做:

customerIdent.getOrElse (None)

编译器说:

[error] C:\Code\...\scala\models\Customer.scala:125: type mismatch;
[error] found : None.type [error] required: models.CustomerIdentification 
[error] customer.get.status, customer. get.rec_marketing_messages, customerIdent.getOrElse (None))

请帮忙

1 个答案:

答案 0 :(得分:0)

您是否能够构建CustomerIdentification的空版本?类似的东西:

val emptyIdent = CustomerIdentification()

然后你可以说:

val customerIdent = custIdentByCustomerId(id).firstOption
customerIdent.getOrElse(emptyIdent)