export
is great! It’s very nice to be able to make wrappers and not have to repeat all declarations on the wrapped type:
trait Foo {
def method(a: Int): String
def method2(a: Int, b: String): Unit = println(method(a) + b)
}
class FooImpl(val suffix: String) extends Foo {
def method(a: Int): String = a.toString + suffix
}
class FooWrapper(foo: Foo) {
export foo.*
}
I believe it’s the case that one can not forward methods defined in extensions. Certainly, the following doesn’t work
trait Foo {
def method(a: Int): String
}
extension (foo: Foo) {
def method2(a: Int, b: String): Unit = println(foo.method(a) + b)
}
class FooImpl(val suffix: String) extends Foo {
def method(a: Int): String = a.toString + suffix
}
class FooWrapper(foo: Foo) {
export foo.*
}
new FooWrapper(new FooImpl).method2(5, "f") // error
This may seem marginal, but it is particularly frustrating for typeclasses, where “member” functions are idiomatically defined using extension methods (IIUC):
trait Foo[T] {
extension (t: T) {
def method(a: Int): String
}
}
class FooImpl(val suffix: String)
given Foo[FooImpl] with {
extension (self: FooImpl) {
def method(a: Int): String = a.toString + self.suffix
}
}
new FooImpl("a").method(5) // works
class FooWrapper[T: Foo](foo: T) {
export foo.* // does not export `method`
}
I think it’s reasonable that export
doesn’t support this out of the box. I’m wondering if there’s a pattern out there I don’t know about or if there’s a chance that something like export foo.extension
could work (as export foo.given
does now). I know that last one is tricky because extension
is not a hard keyword.