1

例如,我具有以下特征:

trait Some {
  def method // i can omit the return type
  val field: Some  // i can't 
}

如果我省略了抽象字段的类型,它会引发编译错误,但在方法的情况下不会?

4

1 回答 1

2

这很简单。如果您省略了抽象方法的返回类型,那么 Scala 编译器会将其推断为Unit(在 typer 阶段之后):

abstract trait Some extends scala.AnyRef {
  def method: Unit;
  <stable> <accessor> def field: String
}
于 2013-07-09T13:20:39.557 回答