The functional difference between the two constructs is that the object Bar
is created only when necessary, and the val Bar
is created as soon as the object Foo
. As a practical matter, this means that you should use an object (or lazy val
) if the right side is expensive and not always necessary. Otherwise val
is probably simpler.
Also note that if the Baz
class is final, you cannot use the object
style since you cannot extend Baz
(although you can still use lazy val
if you want to defer creation until you need it).
Rex kerr
source share