How to design immutable model classes when using inheritance
I'm having trouble finding an elegant way of designing a some simple
classes to represent HTTP messages in Scala.
Say I have something like this:
abstract class HttpMessage(headers: List[String]) {
def addHeader(header: String) = ???
}
class HttpRequest(path: String, headers: List[String])
extends HttpMessage(headers)
new HttpRequest("/", List("foo")).addHeader("bar")
How can I make the addHeader method return a copy of itself with the new
header added? (and keep the current value of path as well)
Thanks, Rob.
No comments:
Post a Comment