type
MyArray = object of RootObj
data: int
proc initMyArray(d: int): MyArray =
result.data = d
proc `+=`(arr: var MyArray, s: int) =
arr.data += s
var arr = initMyArray(5)
arr += 1
echo arr
Yet this code blows up with a compiler error:
type
MyArray = object of RootObj
data: seq[int]
proc initMyArray(d: int): MyArray =
result.data = newSeq[int](10)
for i in 0..<10:
result.data[i] = d
proc `+=`(arr: var MyArray, s: int) =
for i in 0..<10:
arr.data += s
var arr = initMyArray(5)
arr += 1
echo arr
Here is the error:
test.nim(19, 14) Error: type mismatch: got (seq[int], int)
but expected one of:
test.+=(arr: var MyArray, s: int)
system.+=(x: var T, y: T)
system.+=(x: var T, y: T)
error is in the += proc, should be arr.data[i]
proc `+=`(arr: var MyArray, s: int) =
for i in 0..<10:
arr.data[i] += s