tags:

views:

65

answers:

1

In the following example a reference 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?

+8  A: 

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);
      ()
    }
  }
}
Brian Hsu
Good answer. Just a slight correction - you can still use it in the constructor, for instance to print it. As long as the reference is not needed later during the lifetime of the object (e.g. returned by some method), it will not be stored.
axel22