Reputation: 83
I have a data type called Fork
which is immutable. A Fork
instance can include one or many instances of another data type Body
.
I want to insert a list of Body
instances into the same Fork
instance one by one. After each insertion of a Body
, a new instance of Fork
will be returned.
I have tried the following code but it gives an error.
bodies: Seq[Body] // this is from the constructor
val fk = Fork(nw, ne, sw, se) // new instance of a Fork is created
val fkl = bodies.map(b => { val fk: Fork = fk.insert(b) }) // trying to
// insert a list of Body instances to the same instance of Fork
Error:(131, 46) forward reference extends over definition of value fk
val fkl=bodies2.map(b=>{ val fk:Fork=fk.insert(b)})
Upvotes: 1
Views: 41
Reputation: 4342
You could use foldLeft
val fkl = bodies.foldLeft(fk)((f,b) => f.insert(b))
it will start with original fk
instance and then for each body b
will insert body into fork and return updated fork
Upvotes: 4