在 Scala 中是否有更好的方法来提升 PartialFunction?
我偶尔会遇到以下模式,其中我本质上有一个 PartialFunction[SomeType,AnotherType]
,并且希望将其视为 Function[SomeType,Option[AnotherType]
,例如:
def f(s:SomeType):Option[AnotherType] = s match {
case s1:SubType1 => Some(AnotherType(s1.whatever))
case s2:SubType2 => Some(AnotherType(s2.whatever))
case _ => None
}
有没有一种方法可以避免默认情况并将结果包装在定义的 Some
中?到目前为止我想到的最好的办法是:
def f(s:SomeType):Option[AnotherType] = pf.lift(s)
def pf:PartialFunction[SomeType,AnotherType] = {
case s1:SubType1 => AnotherType(s1.whatever)
case s2:SubType2 => AnotherType(s2.whatever)
}
有没有一种方法可以在不定义中间函数的情况下做到这一点?我已经尝试了以下各种方法,但还没有任何东西可以编译:
def f:Function[SomeType,Option[AnotherType]] = {
case s1:SubType1 => AnotherType(s1.whatever)
case s2:SubType2 => AnotherType(s2.whatever)
}.lift
I occasionally come across the following pattern, where I essentially have a PartialFunction[SomeType,AnotherType]
, and want to treat it as a Function[SomeType,Option[AnotherType]
, eg:
def f(s:SomeType):Option[AnotherType] = s match {
case s1:SubType1 => Some(AnotherType(s1.whatever))
case s2:SubType2 => Some(AnotherType(s2.whatever))
case _ => None
}
Is there a way to write the above function in a way that avoids the default case and wrapping the result in Some
where it's defined? The best I've come up with so far is this:
def f(s:SomeType):Option[AnotherType] = pf.lift(s)
def pf:PartialFunction[SomeType,AnotherType] = {
case s1:SubType1 => AnotherType(s1.whatever)
case s2:SubType2 => AnotherType(s2.whatever)
}
Is there a way to do it without defining an intermediate function? I've already tried various things along the lines of the following, but haven't got anything to compile yet:
def f:Function[SomeType,Option[AnotherType]] = {
case s1:SubType1 => AnotherType(s1.whatever)
case s2:SubType2 => AnotherType(s2.whatever)
}.lift
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对象 scala.PartialFunction 中的
condOpt
。来自 scaladoc:condOpt
in object scala.PartialFunction. From the scaladoc:与其说是一个答案,不如说是对为什么 huynhjl 的答案是正确的解释......
你的部分困惑是你试图定义一个部分函数。这一切所做的就是创建一个返回
PartialFunction
对象的方法,您也可以直接创建该对象:虽然我个人更喜欢使用类型归属:
无论哪种方式,您都必须指定输入的内容类型是,因此您必须给出
PartialFunction
的准确签名。我知道感觉应该可以推断出这一点,但遗憾的是事实并非如此!使用归属版本,您可以在同一个位置定义和提升所有内容:
PartialFunction.condOpt
是更好的解决方案,因为它允许推理器为您完成大部分工作,从而更加干净代码 :)Not so much an answer, as an explanation of why huynhjl's answer is correct...
Part of your confusion is that you're trying to
def
a partial function. All this does is to create a method that returns aPartialFunction
object, when you may as well create the thing directly:Though I personally prefer to use type ascription:
Either way, you have to specify what the input type is, so you have to give the exact signature of the
PartialFunction
. I know it feels like it should be possible to to infer this but, alas, that is sadly not the case!Using the ascribed version, you can then define and lift all in the same place:
PartialFunction.condOpt
is the better solution though, as it allows the inferencer to do most of this work for you, leaving much cleaner code :)