【问题标题】:Implicit without explicit imports?没有显式导入的隐式?
【发布时间】:2016-06-25 03:08:56
【问题描述】:

是否可以在没有显式导入的情况下从伴随对象中获取隐含?

我有以下情况:

trait Foo[A] {
  def foo(f: A): String
}

case class Bar(name: String)

object Bar {
  implicit object FooBar extends Foo[Bar] {
    def foo(f: Bar) = f.name
  }
}

class TestImplicits[T] {
  def sayFoo(t: T)(implicit ev: Foo[T]) = ev.foo(t)

  def sayFooIndirect(t: T) = sayFoo(t)
}

我想要实现的是能够实例化 TestImplicits 并调用 sayFooIndirect 方法,而无需将隐式通过它隧道传递到 sayFoo 方法。

val b = new Bar("test")
val t = new TestImplicit[Bar]
t.sayFooIndirect(b)

但是代码无法编译:

could not find implicit value for parameter ev: Foo[T]
     def sayFooIndirect(t: T) = sayFoo(t)
                                      ^

【问题讨论】:

    标签: scala


    【解决方案1】:

    我认为答案是,你不能

    class TestImplicits[T] {
      def sayFoo(t: T)(implicit ev: Foo[T]) = ev.foo(t)
      def sayFooIndirect(t: T) = sayFoo(t)
    }
    

    sayFoo 接受一个参数ev,当您显式或隐式调用它时,必须提供该参数。

    sayFooIndirect 的主体中,没有Foo[T] 类型的隐式对象可用。要使其可用,要么在任何地方隐式传递它,要么使用其他方式将其置于范围内。由于您的类是固定类型为T,因此您可以在构造函数中采用隐式Foo[T]

    class TestImplicits[T](implicit ev: Foo[T]) {
      def sayFoo(t: T) = ev.foo(t)
      def sayFooIndirect(t: T) = sayFoo(t)
    }
    

    使用以下方法将起作用:

    val b = new Bar("test")
    val t = new TestImplicits[Bar]
    t.sayFooIndirect(b)
    

    【讨论】:

    • 谢谢。我太专注于在需要隐式的方法中添加隐式。但是在我的用例中将它添加到构造函数中效果很好。
    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 2022-07-20
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    相关资源
    最近更新 更多