Atol
Atol

Reputation: 599

Update immutable data structure through inheritance

I'm making a strategic game and I try to apply what I learned, try to use immutable data. In my game I have Units, these units can have different special function. By exemple some plane can hide themselves. What I search is a way to be able to do some sort of

abstract class Units {
val life:Int
}

trait Hidable { self: Units => 
val hided:Boolean
def hide:Units with Hidable= ....
}

without having to copy paste:

def hide = copy(hided=true)

on every case class that mixin Hidable.

Upvotes: 2

Views: 291

Answers (1)

George
George

Reputation: 8378

A common way to update an immutable data structure -- is using lenses. There's a compiler plugin to generate lenses for your code, though it is not very production-ready. It is also available for an old scalaz only.

Here's a related question.

Upvotes: 1

Related Questions