I understand that it is a parameter that can be modified be the proc. But does this not apply to whatever the parameter contains if it's an object? This is a stripped down version of the code where I get the issue:
type
ConnGene* = tuple
weight: float
Genome* = object
conn_genes*: seq[ConnGene]
proc mutate(genome: var Genome) =
for cg in genome.conn_genes:
cg.weight += 1.0
Where I get this error:
test.nim(10, 19) Error: type mismatch: got (float, float64)
but expected one of:
system.+=(x: var T, y: T)
system.+=(x: var T, y: T)
Which implies that the elements of the sequence cannot be modified. Is there a way around this, or am I doing something completely wrong?Your problem is not related to the var parameter but about the iterator you use. Your current code implicitly uses the "items()" iterator in the for loop. To be able to modify the elements you get from the iterator you need to use the "mitems()" version (m = modifiy).
This will work as expected by you:
type
ConnGene* = tuple
weight: float
Genome* = object
conn_genes*: seq[ConnGene]
proc mutate(genome: var Genome) =
for cg in mitems(genome.conn_genes):
cg.weight += 1.0
(EDIT: Example on glot.io https://glot.io/snippets/ee1m87sbp6)
To be able to modify the elements you get from the iterator you need to use the "mitems()" version (m = modifiy).
The more you know!
And I used
for i in 0..s.high:
s[i] = ...
before.