Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala:如何返回空对象而不是无_Scala - Fatal编程技术网

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

scala:如何返回空对象而不是无,scala,Scala,我不想返回None,我想返回CustomerIdentification类型的空CustomerIndent 如果我这样做: val customerIdent = custIdentByCustomerId(id).firstOption customerIdent.getOrElse (None) 编者说: customerIdent.getOrElse (None) 请帮助您是否能够构建空版本的CustomerIdentification?比如: [error] C:\Code\...

我不想返回None,我想返回CustomerIdentification类型的空CustomerIndent

如果我这样做:

val customerIdent = custIdentByCustomerId(id).firstOption
customerIdent.getOrElse (None)
编者说:

customerIdent.getOrElse (None)

请帮助

您是否能够构建空版本的CustomerIdentification?比如:

[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))
然后你可以说:

val emptyIdent = CustomerIdentification()

编译时出现此错误:[error]C:\Code\smart check\common\src\main\scala\models\Customer.scala:125:typ e不匹配;找到[错误]:无。需要键入[错误]:models.CustomerIdentification[错误]customer.get.status,customer。GET.ReCySaleMangIn消息,CuffelIdEng.GoRoReNoReRePoE不与您认为您所希望的任何类型类型的空版本无关。我不能创建一个空对象,因为它需要参数:类…[错误]C:\Code\smart check\common\src\main\scala\models\Customer.scala:118:没有足够的参数用于方法apply:id:String、Customer\u id:String、id\u type:S tring、id\u number:String、issuer:String、expiration\u date:java.sql.Datemodels。对象CustomerIdentification中的CustomerIdentification。[错误]未指定的值参数id、客户id、id类型。。。[错误]val emptyIdent=CustomerIdentification[错误]公共/编译:编译失败DAH。好吧,看起来好像没有空的客户标识。您的函数是否可能具有选项[CustomerIdentification]的返回类型而不是CustomerIdentification?这样您就可以返回custIdentityCostMeridid.firstOption。我认为这是正确的方向。如果你能找到一个custId,你可以返回一些custId。如果不能,则不返回任何值。由于不能生成空CustomerId,因此需要使用选项[CustomerId],我有一个带有一些参数的案例类和这个CustomerIndent案例类。类似于case类CustomerValueid:String,first_name:Option[String],customerIdentification:customerIdentification。现在数据库中可能不存在customerIdentification,我返回CustomerValue,但我不能将CustomerValue设置为可选,因为它将始终存在。只有参数customerIdentification可以是可选的,如果找不到,我会尝试将其设置为空,这就是问题所在。如果数据库中可能缺少customerIdentification,那么CustomerValue可能应该包含选项[customerIdentification],或者您应该创建另一个这样的类。让我试试,谢谢
val customerIdent = custIdentByCustomerId(id).firstOption
customerIdent.getOrElse(emptyIdent)