gpt4 book ai didi

class - 我可以在 Scala 匹配语句中使用类变量吗?

转载 作者:行者123 更新时间:2023-12-01 13:53:43 25 4
gpt4 key购买 nike

假设我有这样的东西:

obj match {
case objTypeOne : TypeOne => Some(objTypeOne)
case objTypeTwo : TypeTwo => Some(objTypeTwo)
case _ => None
}

现在我想概括一下,传入一种类型来匹配:

obj match {
case objTypeOne : clazz => Some(objTypeOne)
case objTypeTwo : TypeTwo => Some(objTypeTwo)
case _ => None
}

但这是不允许的,我认为是出于句法而不是语义的原因(尽管我也猜测即使 clazz 是 Class[C] 类型也会被删除,因此 Option 的类型将丢失)。

我最终得到了:

if(clazzOne.isAssignableFrom(obj.getClass)) Some(clazz.cast(obj))
if(obj.isInstanceOf[TypeTwo]) Some(obj.asInstanceOf[TypeTwo])
None

我只是想知道是否有更好的方法。

最佳答案

您可以定义一个提取器来匹配您的对象:

class IsClass[T: Manifest] {
def unapply(any: Any): Option[T] = {
if (implicitly[Manifest[T]].erasure.isInstance(any)) {
Some(any.asInstanceOf[T])
} else {
None
}
}
}

那么让我们测试一下:

class Base { def baseMethod = () }
class Derived extends Base

val IsBase = new IsClass[Base]

def test(a:Any) = a match {
case IsBase(b) =>
println("base")
b.baseMethod
case _ => println("?")
}

test(new Base)
test(1)

您必须为提取器定义一个 val,例如,您不能内联 IsBase。否则它将被解释为提取器。

关于class - 我可以在 Scala 匹配语句中使用类变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4105328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com