0

Is there any purpose for a final object? None of the other uses of final seem to make any sense here:

  • preventing a type from being extended (as in final class) doesn't make sense since object's can't be extended anyways
  • preventing a member from being overriden (as in final val) doesn't make sense since object members are already final

Are Foo1 and Foo2 different in any way? Perhaps in some nested context...?

final object Foo1 { val i = 7 }
object       Foo2 { val i = 7 }
Alec
  • 31,829
  • 7
  • 67
  • 114
  • 1
    Does this answer your question? [What's the point of declaring an object as "final"?](https://stackoverflow.com/questions/26079591/whats-the-point-of-declaring-an-object-as-final) – lukeg Dec 12 '19 at 21:33
  • Technically speaking, they are equivalent. It was even proposed to avoid the first one to compile. But reality is that the `final` helps to iniling so I would `final object Foo1 { final val i: Int = 7 }`. Anyways most of the story can be found [here](https://github.com/scala/bug/issues/11094) _(which also ends in the other question liked by @lukeg)_. – Luis Miguel Mejía Suárez Dec 12 '19 at 21:34
  • @lukeg Yes, it does. Seems like `object` members are _not_ always `final`. – Alec Dec 12 '19 at 21:37

0 Answers0