The following class has an auxillary constructor to change one property in an immutable way.
class AccUnit(size: Long, start: Date, direction:Direction, protocol:String) {
def this(size:Long, that:AccUnit) {this(size, that.start, that.direction, that.protocol)}
}
Compiler returns an errors:
AccUnit.scala:26: error: value start is not a member of trafacct.AccUnit
def this(size:Long, that:AccUnit) {this(size, that.start, that.direction, that.protocol)}
^
AccUnit.scala:26: error: value direction is not a member of trafacct.AccUnit
def this(size:Long, that:AccUnit) {this(size, that.start, that.direction, that.protocol)}
^
AccUnit.scala:26: error: value protocol is not a member of trafacct.AccUnit
def this(size:Long, that:AccUnit) {this(size, that.start, that.direction, that.protocol)}
Why does it think, there is no such members?
,
Because it should be
class AccUnit(val size: Long, val start: Date, val direction:Direction, val protocol:String) {...}
or
case class AccUnit(size: Long, start: Date, direction:Direction, protocol:String) {...}
In your version, size
and others are only constructor arguments, but not members.
UPDATE: You can check it yourself:
// Main.scala
class AccUnit(size: Long, protocol: String)
F:\MyProgramming\raw>scalac Main.scala
F:\MyProgramming\raw>javap -private AccUnit
Compiled from "Main.scala"
public class AccUnit extends java.lang.Object implements scala.ScalaObject{
public AccUnit(long, java.lang.String);
}
,
If you’re using Scala 2.8, then a better solution is to use the copy method defined on case classes, which takes advantage of the named/default parameters feature:
case class AccUnit(size: Long, start: Date, direction:Direction, protocol:String)
val first = AccUnit(...)
val second = first.copy(size = 27L)