【发布时间】:2018-05-22 00:26:25
【问题描述】:
编译器在BroFinder1 和BroFinder2 中看到导致第一个失败的哪些差异?我真的需要 BroFinder1 在不使用 Aux Pattern 等模式的情况下正常工作。
trait Brother[I] {
type Bro
def get: Bro
}
class Foo
object Foo {
implicit object bro extends Brother[Foo] {
override type Bro = Bar
override def get = new Bar
}
}
class Bar {
def barSpecificAction(): Unit = ()
}
class BroFinder1[I](implicit val b: Brother[I]) {
def brotherOf: b.Bro = b.get
}
class BroFinder2[I] {
def brotherOf(implicit b: Brother[I]): b.Bro = b.get
}
new BroFinder1[Foo].brotherOf.barSpecificAction() // Doesn't compile
//error: Error:(73, 32) value barSpecificAction is not a member of _1.b.Bro
//new BroFinder1[Foo].brotherOf.barSpecificAction();
^
new BroFinder2[Foo].brotherOf.barSpecificAction() // Ok
//scala version: 2.12.4
【问题讨论】:
-
请提供错误信息。
-
@RichDougherty 更新了帖子。
标签: scala type-inference generic-programming implicits