Scala:伴随对象与案例类的定义顺序 [英] Scala: order of definition for companion object vs case class

查看:79
本文介绍了Scala:伴随对象与案例类的定义顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala 2.9.1中,我得到以下行为:

In Scala 2.9.1 I get the following behavior:

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

   def bar() {
      object Y         // this compiles
      case class Y()

      case class Z()
      object Z         // won't compile (see below)
   }
}

编译器抱怨Object Z: 错误:Z已被定义为(编译器生成的)案例类伴随对象Z

The compiler complains for Object Z: error: Z is already defined as (compiler-generated) case class companion object Z

如果案例类定义在函数定义内,则似乎不允许在案例类定义之后为案例类定义伴随对象.这是编译器错误,还是故意的?如果是后者,为什么?

It looks as if it is not permissible to define a companion object for a case class after the case class definition if they are within a function definition. Is this a compiler bug, or intentional? If the latter, why?

推荐答案

这是一个已知的错误: SI-3772 :同伴和方法拥有的案例类.这已部分修复,但OP的问题仍然存在.如果要修复,请投票.

This is a known bug: SI-3772: companions and method-owned case classes. This is partially fixed, but the OP's issue still remains. Vote it up if you want it fixed.

这篇关于Scala:伴随对象与案例类的定义顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆