I am confused by this description in “5.1.3 Implicit Resolution” in Joshua Suaret’s Scala book in depth, on page 100:
Scala objects cannot have companion objects for implicits. Because of this, it is implied that it is associated with an object type, which is desirable for the implicit volume of this object type to be provided from an external scope. Here is an example:
scala> object Foo { | object Bar { override def toString = "Bar" } | implicit def b : Bar.type = Bar |} defined module Foo scala> implicitly[Foo.Bar.type] res1: Foo.Bar.type = Bar
But for now, I am making the Bar object implicit in the REPL:
scala> object Foo { | implicit object Bar { | override def toString = "isBar" } | } defined module Foo scala> implicitly[Foo.Bar.type] res0: Foo.Bar.type = isBar
It seems that he does not need to define the implicit in the external realm. Or do I think Joshua is completely wrong?
source share