Scala - 如何使用模式匹配 return Option[String] 而不是 Any?
Scala - How to return Option[String] instead of Any with pattern matching?
我想从 HBase 获取 Sale 对象,并将它们的 HBase id(ImmutableBytesWritable
的字符串表示)连接为 Option[String]
。
首先,我实施了 processSales
方法,因此它只 returned 所有销售 + hBase id,如下所示:
private def processSales (result: Result, hBaseId: String): Option[String] = {
val triedSale: Try[Sale] = myThriftCodec.invert(result.getValue("binary", "object"))
triedSale match {
case Success(sale) => Some(hBaseId + sale)
case _ => None
}
}
现在我只想 return 那些串联的 hBaseIds + Sales
,其中销售额有 metric_1 == null
所以我尝试了以下方法:
private def processSales (result: Result, hBaseId: String): Any = {
val triedSale: Try[Sale] = myThriftCodec.invert(result.getValue("binary", "object"))
triedSale match {
case Success(sale) => Some(hBaseId + sale)
case _ => None
}
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(sale => hBaseId + sale)
}
}
但似乎我在这里遗漏了一些东西和方法 returns Any
即使我这样包装 Option(hBaseId + sale)
.
为了 return Option[String]
的销售额 metric_1 == null
,我应该在我的逻辑中修正什么?
UPD: 不指出我问题的问题就投反对票是没有意义的。它完全阻碍了寻求新知识的积极性。
您在其他情况下缺少匹配案例的第二个选项,因此当度量不为 null 时它是 returning Unit,因此在一种情况下为 Unit,在另一种情况下为 Option(String),编译器猜测你想要 Any as return type
当 metric_1 不为空时,您想 return 做什么?在此示例中,您 return 完全相同的输入:
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(s => hBaseId + s) else someSale
}
或者以更优雅的方式,您可以这样做:
triedSale match {
case Success(metric_1) if metric_1 = null => Some(hBaseId + metric_1)
case Success(metric_1) if metric_1 != null => triedSale
case _ => None
}
编辑
根据评论,当 metric_1 为 null 时,您只想 return 一些东西,所以这是我理解的最佳解决方案:
另外,为什么要对同一个变量进行两次模式匹配?
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(s => hBaseId + s) else None
}
或类似这样的内容:
triedSale match {
case Success(metric_1) if metric_1 = null => Some(hBaseId + metric_1)
case _ => None
}
是不是就这么简单?
myThriftCodec.invert(result.getValue("binary", "object"))
.toOption
.filter(_.metric_1 == null)
.map(hBaseId+_)
我想从 HBase 获取 Sale 对象,并将它们的 HBase id(ImmutableBytesWritable
的字符串表示)连接为 Option[String]
。
首先,我实施了 processSales
方法,因此它只 returned 所有销售 + hBase id,如下所示:
private def processSales (result: Result, hBaseId: String): Option[String] = {
val triedSale: Try[Sale] = myThriftCodec.invert(result.getValue("binary", "object"))
triedSale match {
case Success(sale) => Some(hBaseId + sale)
case _ => None
}
}
现在我只想 return 那些串联的 hBaseIds + Sales
,其中销售额有 metric_1 == null
所以我尝试了以下方法:
private def processSales (result: Result, hBaseId: String): Any = {
val triedSale: Try[Sale] = myThriftCodec.invert(result.getValue("binary", "object"))
triedSale match {
case Success(sale) => Some(hBaseId + sale)
case _ => None
}
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(sale => hBaseId + sale)
}
}
但似乎我在这里遗漏了一些东西和方法 returns Any
即使我这样包装 Option(hBaseId + sale)
.
为了 return Option[String]
的销售额 metric_1 == null
,我应该在我的逻辑中修正什么?
UPD: 不指出我问题的问题就投反对票是没有意义的。它完全阻碍了寻求新知识的积极性。
您在其他情况下缺少匹配案例的第二个选项,因此当度量不为 null 时它是 returning Unit,因此在一种情况下为 Unit,在另一种情况下为 Option(String),编译器猜测你想要 Any as return type
当 metric_1 不为空时,您想 return 做什么?在此示例中,您 return 完全相同的输入:
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(s => hBaseId + s) else someSale
}
或者以更优雅的方式,您可以这样做:
triedSale match {
case Success(metric_1) if metric_1 = null => Some(hBaseId + metric_1)
case Success(metric_1) if metric_1 != null => triedSale
case _ => None
}
编辑
根据评论,当 metric_1 为 null 时,您只想 return 一些东西,所以这是我理解的最佳解决方案:
另外,为什么要对同一个变量进行两次模式匹配?
triedSale match {
case someSale => if (someSale.get.metric_1 = null) someSale.map(s => hBaseId + s) else None
}
或类似这样的内容:
triedSale match {
case Success(metric_1) if metric_1 = null => Some(hBaseId + metric_1)
case _ => None
}
是不是就这么简单?
myThriftCodec.invert(result.getValue("binary", "object"))
.toOption
.filter(_.metric_1 == null)
.map(hBaseId+_)