是否可以为内部类定义扩展方法? 语法参考似乎表明可以有一个
UsingParamClause
beforeDefParam
,这是有前途的,并激发了以下示例:
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
类型的合适给定实例。我需要使用给定的实例吗?