4

Say I define the following:

class A {
    def foo() = println("A::foo")
}

implicit class Wrapper(a: A) {
    def foo() = println("Wrapper::foo")
    def bar() = println("Wrapper::bar")
}

val a = new A
a.foo()
a.bar()

A::foo() is the method that is called. Is there any possible way an implicit class can override the default implementation in A?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
user79074
  • 4,937
  • 5
  • 29
  • 57

1 Answers1

4

If a member method exists then compiler would not search for implicit. If overriding with inheritance is not an option, you might consider "turning the tables" and create a delegate like so

class A {
  def foo() = println("A::foo")
  def zar() = println("A::zar")
}

class Wrapper(val a: A) {
  def foo() = println("Wrapper::foo")
  def bar() = println("Wrapper::bar")
}

implicit def unwrap(wrapped: Wrapper): A = wrapped.a

val a = new Wrapper(new A)
a.zar()   // A::zar
a.foo()   // Wrapper::foo
a.bar()   // Wrapper::bar
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • Can you plz explain what does "implicit def unwrap(wrapped: Wrapper): A = wrapped.a" do? – Salim Jan 25 '20 at 17:58
  • 1
    @Salim It expands `a.zar()` into `unwrap(a).zar()` because `Wrapper` does not have `zar` member but `A` does. This delegates all calls to `A` except for those methods provided in `Wrapper`, such as `foo`, which in effect enables us to "override" `A.foo`. – Mario Galic Jan 25 '20 at 18:11
  • Thanks Mario. Appreciate it. – Salim Jan 25 '20 at 19:32