1

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?

1
  • 2
    Try given val myFoo: Foo = new Foo. Only givens can be summoned for using. Commented Aug 1, 2023 at 2:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.