I'm having problems designing the a class hierarchy in scala for a compiler project. The hierarchy looks as follows:
abstract class TACInstr(index:Int)
case class TAC_Label(label: String,index : Int) extends TACInstr(index)
case class TAC_Jump(label: TAC_Label,index : Int) extends TACInstr(index)
...
Here index
is a variable that represents the order number of the instruction in some listing. It is important that I cannot declare this variable to be immutable at least for the TAC_Label
subclass because it is not always predictable in my code what should be its index when it is declared.
Later on I'm using this index value in the following definition:
def getBlock(i : TACInstr) : BasicBlock =
blocks.find(b => b.instr.index == i.index).get
So basically I'm comparing the equality of two indexes of different instructions. This gives the following error:
Cannot resolve symbol index
because apparently in this way index is private to the classes definitions
My approaches
I tried to declare the hierarchy as follows:
abstract class TACInstr(var index:Int)
case class TAC_Label(label: String,override var index : Int) extends TACInstr(index)
case class TAC_Jump(label: TAC_Label,override var index : Int) extends TACInstr(index)
...
But I'm obtaining the following error type:
Error:... overriding variable index in class TACInstr of type Int;
variable index cannot override a mutable variable
Are there any elegant solutions to this problem? Something better than declaring index
as a member?
The main problem is that you are trying to override a field. The examples below solve the problem:
Now with traits
More scala way
This is not the way of writing things in scala. Many smells are: abstract class, var, parent fields. Code in scala not in its design way will only make it hard.