关于隐式的奇怪错误消息
scala> implicit def transitive[A, B, C](implicit f: A => B, g: B => C): A => C = f andThen g
transitive: [A, B, C](implicit f: A => B, implicit g: B => C)A => C
scala> class Foo; class Bar; class Baz { def lol = println("lol") }
defined class Foo
defined class Bar
defined class Baz
scala> implicit def foo2Bar(f: Foo) = new Bar
foo2Bar: (f: Foo)Bar
scala> implicit def bar2Baz(f: Bar) = new Baz
bar2Baz: (f: Bar)Baz
scala> implicitly[Foo => Baz]
<console>:14: error: diverging implicit expansion for type Foo => Baz
starting with method transitive in object $iw
implicitly[Foo => Baz]
从上面的代码中可以明显看出,我正在尝试编写一个方法,当在作用域中导入时,该方法将使隐式转换具有传递性。我原以为这段代码能工作,但令人惊讶的是它没有。上述错误消息是什么意思,如何使该代码工作?
scala> implicit def transitive[A, B, C](implicit f: A => B, g: B => C): A => C = f andThen g
transitive: [A, B, C](implicit f: A => B, implicit g: B => C)A => C
scala> class Foo; class Bar; class Baz { def lol = println("lol") }
defined class Foo
defined class Bar
defined class Baz
scala> implicit def foo2Bar(f: Foo) = new Bar
foo2Bar: (f: Foo)Bar
scala> implicit def bar2Baz(f: Bar) = new Baz
bar2Baz: (f: Bar)Baz
scala> implicitly[Foo => Baz]
<console>:14: error: diverging implicit expansion for type Foo => Baz
starting with method transitive in object $iw
implicitly[Foo => Baz]
As should be obvious from the above code, I am trying to write a method which when imported in scope will make implicit conversions transitive. I was expecting this code to work, but surprisingly it doesn't. What does the above error message mean, and how can I make this code work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:如评论中所述,此方法无法在 2.8 上编译,而
隐式[Foo =>; Baz]
可以正常工作,而(new Foo).lol
则不能。如果您将
transitive
重命名为conforms
以隐藏Predef
中的方法,则效果很好:请参阅 这个答案了解更多详情。
附带说明:使用
-Xlog-implicits
启动 REPL 是在这种情况下获取更多信息性错误消息的便捷方法。在这种情况下,一开始并没有太多帮助:但是如果我们暂时将
foo2Bar
和bar2Baz
重写为函数,我们会收到一条错误消息,突出显示了歧义:很明显,我们只需要影子
conforms
。Update: As noted in the comments, this approach doesn't compile on 2.8, and while
implicitly[Foo => Baz]
works correctly,(new Foo).lol
doesn't.This works just fine if you rename your
transitive
toconforms
to shadow the method inPredef
:See this answer for more details.
As a side note: starting the REPL with
-Xlog-implicits
is a handy way to get more informative error messages in situations like this. In this case it's not a lot of help at first:But if we temporarily rewrite
foo2Bar
andbar2Baz
to be functions, we get an error message that highlights the ambiguity:Now it's clear that we just need to shadow
conforms
.