I\'ve recently created an immutable class supporting operations like +, -, etc. that returns a new instance of that class when it is changed.
I wanted to make a subclass
You could use an implementation trait, like the collection classes do, which is parametrized by the concrete type. E.g., something like:
trait FooLike[+A] {
protected def bar: Int
protected def copy(newBar: Int): A
def +(other: Foo): A = copy(bar + other.bar)
}
class Foo(val bar: Int) extends FooLike[Foo] {
protected def copy(newBar: Int): Foo = new Foo(newBar)
}
class Subclass(barbar: Int) extends Foo(barbar) with FooLike[Subclass] {
protected def copy(newBar: Int): Subclass = new Subclass(newBar)
}