Appending a label to immutable case classes in Scala?

It is entirely possible to create your own copy-like method: abstract class Core(val label: OptionString) { def set(label: OptionString): Core } class Impl(label: OptionString = None) extends Core(label) { def set(label: OptionString = this. Label) = new Impl(label) } used thusly: scala> val I = new Impl i: Impl = Impl@1930ebb scala> i. Label res0: OptionString = None scala> i.

Set(label = Some("thing")) res1: Impl = Impl@b28f30 scala> res1. Label res2: OptionString = Some(thing) But, pragmatically, I wouldn't be too quick to dismiss the use of vars here. It is easier to reason about immutable values, but the ones you get are pretty well isolated within the parser, as far as I can tell.An alternative idea would be to create a method that converts everything to an immutable version at the end, or if the parser code ends up storing all the data elsewhere anyway, just leaving it mutable Another way to go is to make the abstract class not abstract, but to actually make it a case class.

You can derive classes from a case class (deriving case classes from case classes is a no-no, however). The trick then would be to make your variable data that you may need to preserve live in a field: abstract class SpecializedStuff { } case class ParticularStuff(val i: Int) extends SpecializedStuff case class Core(details: SpecializedStuff, label: OptionString = None) class Impl(p: ParticularStuff) extends Core(p) scala> val I = new Impl( ParticularStuff(5) ) i: Impl = Core(ParticularStuff(5),None) scala> i. Copy(label = Some("thing")) res0: Core = Core(ParticularStuff(5),Some(thing)).

It is entirely possible to create your own copy-like method: abstract class Core(val label: OptionString) { def set(label: OptionString): Core } class Impl(label: OptionString = None) extends Core(label) { def set(label: OptionString = this. Label) = new Impl(label) } used thusly: scala> val I = new Impl i: Impl = Impl@1930ebb scala> i. Label res0: OptionString = None scala> i.

Set(label = Some("thing")) res1: Impl = Impl@b28f30 scala> res1. Label res2: OptionString = Some(thing) But, pragmatically, I wouldn't be too quick to dismiss the use of vars here. It is easier to reason about immutable values, but the ones you get are pretty well isolated within the parser, as far as I can tell.An alternative idea would be to create a method that converts everything to an immutable version at the end, or if the parser code ends up storing all the data elsewhere anyway, just leaving it mutable.

Another way to go is to make the abstract class not abstract, but to actually make it a case class. You can derive classes from a case class (deriving case classes from case classes is a no-no, however). The trick then would be to make your variable data that you may need to preserve live in a field: abstract class SpecializedStuff { } case class ParticularStuff(val i: Int) extends SpecializedStuff case class Core(details: SpecializedStuff, label: OptionString = None) class Impl(p: ParticularStuff) extends Core(p) scala> val I = new Impl( ParticularStuff(5) ) i: Impl = Core(ParticularStuff(5),None) scala> i.

Copy(label = Some("thing")) res0: Core = Core(ParticularStuff(5),Some(thing)).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions