如何检查值的可见类型

发布于 2024-12-17 03:25:50 字数 628 浏览 0 评论 0原文

我有这样的事情:

def my_check[T](arg: T) = arg match {
  case x: GenTraversable[_] = ...  // XX
  case x: Array[_]
   ...

我想合并一些 cases 表达式:
在上面标有 XX 的代码中,我想检查是否 arg <: GenTraversable[_]arg <% GenTraversable[-]。例如, Array 类型也可以匹配这种情况(存在从 Array 到 WrapperArray <: GenTraversable 的隐式转换)

我不知道不想在解决方案中使用异常,所以我对以下内容不感兴趣:

try:
  val trv: GenTraversable = arg
catch ...

我在 my_check 函数中尝试过类似

T <:< GenTraversable        // error: not found: value T

但编译器抱怨。

I have something like this:

def my_check[T](arg: T) = arg match {
  case x: GenTraversable[_] = ...  // XX
  case x: Array[_]
   ...

I want to merge some cases expression:
In the code above marked with XX, I want to check if arg <: GenTraversable[_] or arg <% GenTraversable[-]. For example, the Array type could match this case as well (there exists implicit conversion from Array to WrapperArray <: GenTraversable)

I don't want to use exception in the solution, so I'm not interesting in something like:

try:
  val trv: GenTraversable = arg
catch ...

I've tried in my_check function something like

T <:< GenTraversable        // error: not found: value T

But compiler complain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不喜欢何必死缠烂打 2024-12-24 03:25:50

隐式解析(<% 包括)是编译时的事情,基于编译时已知的类型。

在您的例程中,编译时唯一知道的有关 arg 的信息是它的类型为 T,这几乎什么都没有。您可能会遇到类似这样的情况

def check[T <% GenTraversable[_](arg: T) = ...

,但是,只有在编译时已知 argGenTraversable 时,您的例程才能被调用。

我不知道你在哪里写了T <:< GenTraversable[_](可能在需要值的地方)。要获得从类型 AB 的隐式转换,您可以编写 implicitly[A =>; B],但同样,如果在编译时无法解析,它将失败。所以这里没有任何帮助,因为 T 是一个不受约束的泛型参数。

Implicit resolution (<% included) is a compile time thing, based on the types as they are known at compile time.

In your routine, the only thing that is known at compile time about arg is that it is of type T, which is about nothing. You might have something such as

def check[T <% GenTraversable[_](arg: T) = ...

This is definitely not the same thing however your routine could only be called if arg is known at compile time to be a GenTraversable.

I don't know where you wrote T <:< GenTraversable[_] (probably in a place where a value is expected). To get the implicit conversion from types A to B, you can write implicitly[A => B], but again, it will fail if it cannot be resolved at compile time. So it would not be any help here, with T an unconstrained generic parameter.

鲜血染红嫁衣 2024-12-24 03:25:50

我不确定我是否理解你的问题。这段代码对我有用:

class X[T] (name: T)

class Y[T] (_name: T, age: Int) extends X[T](_name)

def foo[T](arg: T) = arg match {
  case a: Y[_] => println("y " )
  case a: X[_] => println("x " )
  case _ => println("?")
}

val y = new Y("Olle", 3);
foo(y)

如果 Y 是 X 的子类(如本例所示),那么您必须从 Y 开始,然后再采用更通用的类。请再次解释一下问题...?

I am not sure I understand your question. This code works for me:

class X[T] (name: T)

class Y[T] (_name: T, age: Int) extends X[T](_name)

def foo[T](arg: T) = arg match {
  case a: Y[_] => println("y " )
  case a: X[_] => println("x " )
  case _ => println("?")
}

val y = new Y("Olle", 3);
foo(y)

If Y is a subclass of X (as in this case) then you must start with Y and take the more generic classes later on. Please explain the question once more...?

躲猫猫 2024-12-24 03:25:50

我很确定这是不可能的。模式匹配发生在运行时,隐式和非隐式之间的区别仅存在于编译时。

I am pretty sure this isn't possible. Pattern matching happens at runtime, and distinction between implicits and non-implicits only exists at complile-time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文