关于隐式的奇怪错误消息

发布于 2024-12-12 07:41:57 字数 780 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

一曲爱恨情仇 2024-12-19 07:41:57

更新:如评论中所述,此方法无法在 2.8 上编译,而隐式[Foo =>; Baz] 可以正常工作,而 (new Foo).lol 则不能。


如果您将 transitive 重命名为 conforms 以隐藏 Predef 中的方法,则效果很好:

implicit def conforms[A, B, C](implicit f: A => B, g: B => C): A => C = f andThen g

请参阅 这个答案了解更多详情。


附带说明:使用 -Xlog-implicits 启动 REPL 是在这种情况下获取更多信息性错误消息的便捷方法。在这种情况下,一开始并没有太多帮助:

scala> implicitly[Foo => Baz]
scala.this.Predef.conforms is not a valid implicit value for Foo => Baz because:
type mismatch;
 found   : <:<[Foo,Foo]
 required: Foo => Baz
<console>:14: error: diverging implicit expansion for type Foo => Baz
starting with method transitive in object $iw
              implicitly[Foo => Baz]
                        ^
scala.this.Predef.conforms is not a valid implicit value for Foo => Baz because:
type mismatch;
 found   : <:<[Foo,Foo]
 required: Foo => Baz
transitive is not a valid implicit value for Unit => Foo => Baz because:
not enough arguments for method transitive: (implicit f: A => B, implicit g: B => C)A => C.
Unspecified value parameter g.
transitive is not a valid implicit value for => Unit => Foo => Baz because:
not enough arguments for method transitive: (implicit f: A => B, implicit g: B => C)A => C.
Unspecified value parameter g.

但是如果我们暂时将 foo2Barbar2Baz 重写为函数,我们会收到一条错误消息,突出显示了歧义

implicit val foo2Bar = (_: Foo) => new Bar
implicit val bar2Baz = (_: Bar) => new Baz

scala> implicitly[Foo => Baz]
transitive is not a valid implicit value for Foo => Baz because:
ambiguous implicit values:
 both method conforms in object Predef of type [A]=> <:<[A,A]
 and value foo2Bar in object $iw of type => Foo => Bar
 match expected type Foo => B

:很明显,我们只需要影子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 to conforms to shadow the method in Predef:

implicit def conforms[A, B, C](implicit f: A => B, g: B => C): A => C = f andThen g

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:

scala> implicitly[Foo => Baz]
scala.this.Predef.conforms is not a valid implicit value for Foo => Baz because:
type mismatch;
 found   : <:<[Foo,Foo]
 required: Foo => Baz
<console>:14: error: diverging implicit expansion for type Foo => Baz
starting with method transitive in object $iw
              implicitly[Foo => Baz]
                        ^
scala.this.Predef.conforms is not a valid implicit value for Foo => Baz because:
type mismatch;
 found   : <:<[Foo,Foo]
 required: Foo => Baz
transitive is not a valid implicit value for Unit => Foo => Baz because:
not enough arguments for method transitive: (implicit f: A => B, implicit g: B => C)A => C.
Unspecified value parameter g.
transitive is not a valid implicit value for => Unit => Foo => Baz because:
not enough arguments for method transitive: (implicit f: A => B, implicit g: B => C)A => C.
Unspecified value parameter g.

But if we temporarily rewrite foo2Bar and bar2Baz to be functions, we get an error message that highlights the ambiguity:

implicit val foo2Bar = (_: Foo) => new Bar
implicit val bar2Baz = (_: Bar) => new Baz

scala> implicitly[Foo => Baz]
transitive is not a valid implicit value for Foo => Baz because:
ambiguous implicit values:
 both method conforms in object Predef of type [A]=> <:<[A,A]
 and value foo2Bar in object $iw of type => Foo => Bar
 match expected type Foo => B

Now it's clear that we just need to shadow conforms.

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