define in companion object or as inner class
Is there an (dis)advantage (for example performance-wise) for defining classes auxiliary to a trait in its companion object over defining them as inner classes ? For example:
object Foo {
final class Bar[A, B](val x: A, val y: B)
}
trait Foo[A, B] {
def foo: Foo.Bar[A, B]
}
vs.
trait Foo[A, B] {
final class Bar(val x: A, val y: B)
def foo: Bar
}
I tend to use the first variant—because it does not require access to any member of Foo
—but since the type parameters become redundant, I'm thinking about switching to the second variant. Class Bar
is only used internally, so could get a protected
modifier.
I reckon the internal class with have an invisible Foo.this
member somewhere, but then my understanding is that Foo.Bar
is also not purely static as in Java but probably has some pointer to Foo$.this
, too.
上一篇: 对匿名对象的scala反射
下一篇: 在同伴对象中定义或作为内部类