You can do this with a self type:
trait Container[+A] { self =>
def a: A
def methodWithSideEffect(): self.type = {
// perform some side effecting work
this
}
}
class IntContainer(val a: Int) extends Container[Int]
...
val x: IntContainer = new IntContainer(42).methodWithSideEffect()
Or simply with this.type
:
trait Container[+A] {
def a: A
def methodWithSideEffect(): this.type = {
// perform some side effecting work
this
}
}
class IntContainer(val a: Int) extends Container[Int]