I tried the following code. There is a proc "f" which first argument is typedesc T. I want to save some object accoding to T. The proc "enhance" manages the object using global variable.
In the code, I called f by T = int twice, first one is int.f(3) and second one is int.f(4). First one changes global object "Object o". But in the second one, the change is ignored... I also make another global int "dummy" and changed. This change is not ignored. What is happening!?
I also noticed that this code works well in 1.6.14. So, something changed in 2.0.
type Object*[T] = object
s:seq[T]
proc enhance(T:typedesc, k:int):auto {.discardable.} =
echo "enhance is called"
var
o{.global.} = Object[T]()
dummy{.global.} = 0
echo "dummy = ", dummy
echo "o.s = ", o.s
dummy = 2024
o.s = newSeq[T](5)
echo "o.s is changed to ", o.s
return o.addr
proc f*(T:typedesc, k:int):auto = T.enhance(k)[].s[k]
echo int.f(3)
echo float.f(2)
echo int.f(4)
The output for 2.0.2 is
nim
enhance is called
dummy = 0
o.s = @[]
o.s is changed to @[0, 0, 0, 0, 0]
0
enhance is called
dummy = 0
o.s = @[]
o.s is changed to @[0.0, 0.0, 0.0, 0.0, 0.0]
0.0
enhance is called
dummy = 2024
o.s = @[]
o.s is changed to @[0, 0, 0, 0, 0]
0
The output for 1.6.16 is
nim
enhance is called
dummy = 0
o.s = @[]
o.s is changed to @[0, 0, 0, 0, 0]
0
enhance is called
dummy = 0
o.s = @[]
o.s is changed to @[0.0, 0.0, 0.0, 0.0, 0.0]
0.0
enhance is called
dummy = 2024
o.s = @[0, 0, 0, 0, 0]
o.s is changed to @[0, 0, 0, 0, 0]
0