Scala视图应用程序难题 [英] Scala view application puzzler

查看:78
本文介绍了Scala视图应用程序难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们具有以下两个特征:

Say we have the following two traits:

trait Foo[A] { def howMany(xs: List[A]) = xs.size }
trait Bar

从第二个到第一个的隐式转换:

And an implicit conversion from the second to the first:

implicit def bar2foo[A](bar: Bar) = new Foo[A] {}

我们创建一个Bar和一个整数列表:

We create a Bar and a list of integers:

val bar = new Bar {}
val stuff = List(1, 2, 3)

现在,我希望以下方法能起作用:

Now I'd expect the following to work:

bar howMany stuff

但不是:

scala> bar howMany stuff
<console>:13: error: type mismatch;
 found   : List[Int]
 required: List[A]
              bar howMany stuff
                          ^

因此,我们转到规范,其中包含说(粗体字是我的):

So we go to the spec, which has this to say (emphasis in bold is mine):

视图在三种情况下适用.

Views are applied in three situations.

  1. [这里不相关.]

  1. [Isn't relevant here.]

在类型为 T e 的选择 em 中,如果选择器 m 不表示 T 的成员.在这种情况下,将搜索视图 v 适用于 e ,其结果包含名为 m 的成员.这 搜索过程与隐式参数相同,其中 隐式作用域是 T 之一.如果找到这样的视图, 选择 e.m 转换为 v(e).m .

In a selection e.m with e of type T, if the selector m does not denote a member of T. In this case, a view v is searched which is applicable to e and whose result contains a member named m. The search proceeds as in the case of implicit parameters, where the implicit scope is the one of T. If such a view is found, the selection e.m is converted to v(e).m.

在类型为 T e 的选择 em(args)中,如果选择器 m为,则为 表示 T 的某些成员,但是这些成员均不适用于参数 args .在这种情况下,将搜索视图 v 适用于 e 且其结果包含方法 m 适用于 args .进行搜索的情况与 隐式参数,其中隐式范围是 T 之一.如果 找到这样的视图后,选择 e.m 被转换为 v(e).m(args).

In a selection e.m(args) with e of type T, if the selector m denotes some member(s) of T, but none of these members is applicable to the arguments args. In this case a view v is searched which is applicable to e and whose result contains a method m which is applicable to args. The search proceeds as in the case of implicit parameters, where the implicit scope is the one of T. If such a view is found, the selection e.m is converted to v(e).m(args).

因此,我们尝试以下操作,认为它必须太荒谬而无法工作:

So we try the following, thinking it must be too absurd to work:

trait Foo[A] { def howMany(xs: List[A]) = xs.size }
trait Bar { def howMany = throw new Exception("I don't do anything!") }

implicit def bar2foo[A](bar: Bar) = new Foo[A] {}

val bar = new Bar {}
val stuff = List(1, 2, 3)

但是确实如此(至少在2.9.2和2.10.0-RC2上):

But it does (on both 2.9.2 and 2.10.0-RC2, at least):

scala> bar howMany stuff
res0: Int = 3

这会导致一些非常奇怪的行为,例如,

This leads to some really strange behavior, as for example in this workaround for this problem.

我有三个(密切相关的)问题:

I have three (closely related) questions:

  1. 是否存在一种简单的方法(即不涉及使用适当名称添加假方法的方法),以在上述原始情况下正确应用视图?
  2. 有人可以阅读说明此行为的规范吗?
  3. 假设这是预期的行为,那有什么意义吗?

我也很感谢与以前有关此问题的讨论的任何链接-我在Google方面运气不佳.

I'd also appreciate any links to previous discussions of this issue—I haven't been having much luck with Google.

推荐答案

供所有人参考,这仅是一个bug.您所知道的是错误消息:

For everyone's reference, this could only be a bug. The way you know that is the error message:

<console>:13: error: type mismatch;
 found   : List[Int]
 required: List[A]

List [A]不是实类型-它是List应用于其自己的type参数.这不是必需的类型,因为它不是可以表达的类型.

List[A] is not a real type - it is List applied to its own type parameter. That is not a type which can be required since it is not a type which can be expressed.

与此相关的票证是 https://issues.scala-lang.org/浏览/SI-6472 .

这篇关于Scala视图应用程序难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆