0

Which of the following ways is better/preferred? How are they different?

trait MyService {
  def foo: String
}

class MyTest {
  // variant 1
 object fooService1 extends MyService {
   val foo = "foo1"
 }
 // variant 2
 val fooService2 = new MyService {
   val foo = "foo2"
 }
 // (...)
}
ale64bit
  • 6,232
  • 3
  • 24
  • 44

1 Answers1

2

No practical difference, except that if you extend MyTest you will be able to override the val but not the object, [EDIT] and the object is instantiated lazily.

Also, possible duplicate of this.

Community
  • 1
  • 1
John K
  • 1,285
  • 6
  • 18