gpt4 book ai didi

scala - 隐式转换不明确,因此不适用

转载 作者:行者123 更新时间:2023-12-02 10:46:54 27 4
gpt4 key购买 nike

class Foo

implicit def fromInt[A <% Int](x: A) = new Foo // #1
implicit def fromString[A <% String](x: A) = new Foo // #2

0: Foo


error: type mismatch;
found : Int(0)
required: this.Foo
Note that implicit conversions are not applicable because they are ambiguous:
both method fromInt of type [A](x: A)(implicit evidence$1: A => Int)this.Foo
and method fromString of type [A](x: A)(implicit evidence$2: A => String)this.Foo
are possible conversion functions from Int(0) to this.Foo
0: Foo
^

我不确定我是否理解。

如果删除#2,它将编译。

如果删除#1,则无法编译
error: No implicit view available from Int => String.
0: Foo
^

没有歧义。 fromString不是从 IntFoo的转换函数。

为何编译器声称存在歧义?

最佳答案

这似乎是2.9.x和2.10.x中的错误。

在2.11.2中已修复。

https://issues.scala-lang.org/browse/SI-8857

关于scala - 隐式转换不明确,因此不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com