Scala:参数化中完全限定类名的模式匹配问题

发布于 2024-12-04 15:01:51 字数 356 浏览 1 评论 0原文

当使用完全限定的类名参数化 Scala 中的对象时,我在模式匹配时遇到了一些问题。这是基于 Scala 2.9.0.1。有人知道这段代码有什么问题吗?

scala> "foo" match {
 | case y : Seq[Integer] =>
 | case y : Seq[java.lang.Integer] =>
<console>:3: error: ']' expected but '.' found.
   case y : Seq[java.lang.Integer] =>

为什么第一个版本可以工作,而后者却失败了?该问题似乎仅在使用完全限定的类名进行参数化时才会出现。

I have a little problem in pattern matching an object in Scala when it is parameterized with a fully qualified class name. This is based on Scala 2.9.0.1. Anyone knows what's wrong with this code?

scala> "foo" match {
 | case y : Seq[Integer] =>
 | case y : Seq[java.lang.Integer] =>
<console>:3: error: ']' expected but '.' found.
   case y : Seq[java.lang.Integer] =>

Why does the first version work, but the latter fail? The problem only seems to occur when a fully qualified classname is used for the parameterization.

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

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

发布评论

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

评论(2

嘿看小鸭子会跑 2024-12-11 15:01:52

事实上你的第一个例子也不起作用。如果您在 -unchecked 的情况下运行 REPL,您将看到以下错误:

警告:类型模式 Seq[Integer] 中的非变量类型参数 Integer 未选中,因为它已被擦除消除

所以您实际上无法执行您想要执行的操作 - 在运行时,List[Integer] 之间没有区别和一个 List[AnythingElse],所以你不能对其进行模式匹配。您也许可以使用清单来执行此操作,请参阅 http://ofps.oreilly .com/titles/9780596155957/ScalasTypeSystem.html#Manifestshttp://www.scala-blogs.org/2008/10/manifests -reified-types.html

In fact your first example doesn't work either. If you run the REPL with -unchecked, you'll see the following error:

warning: non variable type-argument Integer in type pattern Seq[Integer] is unchecked since it is eliminated by erasure

So you can't actually do what you are trying to do - at run-time there's no difference between a List[Integer] and a List[AnythingElse], so you can't pattern-match on it. You might be able to do this with a Manifest, see http://ofps.oreilly.com/titles/9780596155957/ScalasTypeSystem.html#Manifests and http://www.scala-blogs.org/2008/10/manifests-reified-types.html

一杯敬自由 2024-12-11 15:01:51

根据 Scala 语言规范,第 8.1 节模式,: 后面的标识符需要是称为类型模式,在第 8.2 节中定义:

类型模式由类型、类型变量和通配符组成。 A型
模式 T 具有以下形式之一:

...

参数化类型模式 T [a(1), . 。 。 , a(n)],其中 a(i) 是
输入变量模式或通配符_。此类型模式匹配所有
对于该类型的某些任意实例化,与 T 匹配的值
变量和通配符。这些类型的边界或别名类型
变量的确定如(§8.3)中所述。

...

类型变量模式是一个简单的标识符,以
小写字母。然而,预定义的基本类型别名
unit、boolean、byte、short、char、int、long、float 和 double 不是
归类为类型变量模式。

因此,从语法上讲,您不能在此位置使用完全限定的类作为类型变量模式。但是,您可以使用类型别名,因此:

type JavaInt = java.lang.Integer
List(new java.lang.Integer(5)) match {
    case y: Seq[JavaInt] => 6
    case _ => 7
}

将按预期返回 6。问题在于,正如 Alan Burlison 指出的那样,以下代码也返回 6:

List("foobar") match {
    case y: Seq[JavaInt] => 6
    case _ => 7
}

因为类型正在被删除。您可以通过运行 REPL 或带有 -unchecked 选项的 scalac 来查看这一点。

From the Scala Language Specification, section 8.1 Patterns, the identifier after the : needs to be what is referred to as a Type Pattern, defined in Section 8.2:

Type patterns consist of types, type variables, and wildcards. A type
pattern T is of one of the following forms:

...

A parameterized type pattern T [a(1), . . . , a(n)], where the a(i) are
type variable patterns or wildcards _. This type pattern matches all
values which match T for some arbitrary instantiation of the type
variables and wildcards. The bounds or alias type of these type
variable are determined as described in (§8.3).

...

A type variable pattern is a simple identifier which starts with a
lower case letter. However, the predefined primitive type aliases
unit, boolean, byte, short, char, int, long, float, and double are not
classified as type variable patterns.

So, syntactically, you can't use a fully qualified class as a type variable pattern IN THIS POSITION. You can however, use a type alias, so:

type JavaInt = java.lang.Integer
List(new java.lang.Integer(5)) match {
    case y: Seq[JavaInt] => 6
    case _ => 7
}

will return 6 as expected. The problem is that as Alan Burlison points out, the following also returns 6:

List("foobar") match {
    case y: Seq[JavaInt] => 6
    case _ => 7
}

because the type is being erased. You can see this by running the REPL, or scalac with the -unchecked option.

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