Extension Methods for Inner Class?

46 Views Asked by At

Is it possible to define an extension method for an inner class? The syntax reference seems to show it is possible to have a UsingParamClause before the DefParam, which is promising, and motivates the following example:

class Foo:
  class Bar

extension (using foo: Foo)(bar: foo.Bar) def baz: String = "hi"

val myFoo: Foo = new Foo
val myBar: myFoo.Bar = new myFoo.Bar

val str: String = myBar.baz()

The last line gives me the following error:

value baz is not a member of myFoo.Bar.
An extension method was tried, but could not be fully constructed:

    baz(/* missing */summon[Foo])(myBar)

    failed with:

        No given instance of type Foo was found for parameter foo of method baz

I would expect that myFoo would be a suitable given instance of type Foo. Do I need to use a given instance?

0

There are 0 best solutions below