I'm wondering why the iterator mitems with an or'ed type doesn't compile. https://play.nim-lang.org/#ix=3Dey
The error originates from line 32 when using an UncheckedArray: expression has no address.
If you comment out the last mitems and uncomment the two above, it works fine. I'm trying to understand why UncheckedArray[T] works separately but not together with ptr T, but ptr T does work.
proc `+`*[T; S: SomeInteger](p: ptr T, offset: S): ptr T =
return cast[ptr T](cast[ByteAddress](p) +% (int(offset) * sizeof(T)))
proc `[]`*[T; S: SomeInteger](p: ptr T, offset: S): var T =
return (p + offset)[]
#[
# this compiles and works when separate
iterator mitems*[T](a: var UncheckedArray[T], len: SomeInteger): var T =
for i in 0..<len:
yield a[i]
iterator mitems*[T](a: ptr T, len: SomeInteger): var T =
for i in 0..<len:
yield a[i]
]#
# does not work with UncheckedArray[T], but does work with ptr T
iterator mitems*[T](a: var UncheckedArray[T] | ptr T, len: SomeInteger): var T =
for i in 0..<len:
yield a[i]
var b = cast[ptr int](alloc0(sizeof(int) * 4))
for i in mitems(b, 4):
inc i
doAssert(b[0] == 1)
dealloc(b)
var a = cast[ptr UncheckedArray[int]](alloc0(sizeof(int) * 4))
for i in mitems(a[], 4):
inc i
doAssert(a[0] == 1)
dealloc(a)