Scala: order of definition for companion object vs case class

后端 未结 2 2060
不思量自难忘°
不思量自难忘° 2021-02-12 14:41

In Scala 2.9.1 I get the following behavior:

class Foo {
   case class X()
   object X            // this compiles

   def bar() {
      object Y         // this         


        
2条回答
  •  借酒劲吻你
    2021-02-12 15:27

    The reason why the first is allowed and the second is not is that classes and objects can have forward definitions, but definitions cannot. So why it is possible for the compiler to mix object X with the one defined by the case class, it is not possible to do so in the second case.

    I wonder what happens in the Y case: shadowing or the object companion does not get generated at all?

提交回复
热议问题