开发者

Parent fields initialization

开发者 https://www.devze.com 2023-01-20 18:31 出处:网络
In the following example a refere开发者_Python百科nce to reason is stored in both parent and child. I\'d like to avoid this and store the reference only in parent (usual use of Java exceptions).

In the following example a refere开发者_Python百科nce to reason is stored in both parent and child. I'd like to avoid this and store the reference only in parent (usual use of Java exceptions).

import java.lang.{Exception, Throwable}
class FileError(message: String, reason:Throwable) extends Exception(message, reason) {
  ...
}

How do I initialize a parent class field without storing a value in the child class object?


If you never use reason in the FieldError class, then it will not be stored in FieldError.

import java.lang.{Exception, Throwable}
class FileError(message: String, reason:Throwable) extends Exception(message, reason)

--

brianhsu@NBGentoo ~ $ scalac -print test.scala 
[[syntax trees at end of cleanup]]// Scala source: test.scala
package <empty> {
  class FileError extends java.lang.Exception with ScalaObject {
    def this(message: java.lang.String, reason: java.lang.Throwable): FileError = {
      FileError.super.this(message, reason);
      ()
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消