I am new to Scala but an experienced programmer trying to shed old habits and get on board with functional style. A simple program I'm designing involves questions / answer objects and retains some basic facts about success. This alone would be easy enough using immutable objects - but part of my objective is to link associated facts, so the program might ask them once in awhile next to each other. (Example: "The Knight Rider Car was?" , "What TV show features an Impala? )
In Java I might have just linked associated objects like I've written below, but immutable laws prevent this (I can't link A-> B and instantiate B -> A unless I do it in one shot?)
And I must admit, this is probably a good thing - maybe functional style is designed to prevent this kind of circular referencing. But I'm at a loss how to correct the problem. My design would fail once a question is asked - for this returns a copy object with new values of "right , asked" so object linking seems like a pitfall again. My best guess is to build relation sets in a helper object, based on Q/A data only, but I'd like to avoid juggling a separate data pattern object.
Thank you all - Tim in Columbus Ohio
case class FactItem (
q:String, a:String,
right: Double = 0.0,
asked: Double = 0.0,
linked: List[FactItem] = List[FactItem]()
) {
def rP = right / asked //right per
def subLink( l:List[FactItem] ) : FactItem = {
this.copy( linked = l-this)
}