内部类的扩展方法?

问题描述 投票:0回答:0

是否可以为内部类定义扩展方法? 语法参考似乎表明可以有一个

UsingParamClause
before
DefParam
,这是有前途的,并激发了以下示例:

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()

最后一行给了我以下错误:

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

我希望

myFoo
Foo
类型的合适给定实例。我需要使用给定的实例吗?

scala extension-methods inner-classes dependent-type scala-3
© www.soinside.com 2019 - 2024. All rights reserved.