1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
discard """
nimout: '''
true
true
[nil, nil, nil, nil]
[MyObjectRef(123, 321), nil, nil, nil]
['A', '\x00', '\x00', '\x00']
MyObjectRef(123, 321)
(key: 8, val: 0)
'''
output: '''
true
true
[nil, nil, nil, nil]
[MyObjectRef(123, 321), nil, nil, nil]
['A', '\x00', '\x00', '\x00']
MyObjectRef(123, 321)
'''
"""
type
MyObjectRef = ref object
a,b: int
MyContainerObject = ref object
member: MyObjectRef
MySuperContainerObject = ref object
member: MyContainerObject
arr: array[4, MyObjectRef]
MyOtherObject = ref object
case kind: bool
of true:
member: MyObjectRef
else:
discard
proc `$`(arg: MyObjectRef): string =
result = "MyObjectRef("
result.addInt arg.a
result.add ", "
result.addInt arg.b
result.add ")"
proc foobar(dst: var MyObjectRef) =
dst = new(MyObjectRef)
dst.a = 123
dst.b = 321
proc changeChar(c: var char) =
c = 'A'
proc test() =
# when it comes from a var, it works
var y: MyObjectRef
foobar(y)
echo y != nil
# when it comes from a member, it fails on VM
var x = new(MyContainerObject)
foobar(x.member)
echo x.member != nil
# when it comes from an array, it fails on VM
var arr: array[4, MyObjectRef]
echo arr
foobar(arr[0])
echo arr
var arr2: array[4, char]
changeChar(arr2[0])
echo arr2
var z = MyOtherObject(kind: true)
foobar(z.member)
echo z.member
# this still doesn't work
# var sc = new(MySuperContainerObject)
# sc.member = new(MyContainerObject)
# foobar(sc.member.member)
# echo sc.member.member
# foobar(sc.arr[1])
# echo sc.arr
#var str = "---"
#changeChar(str[1])
#echo str
test()
static:
test()
type T = object
f: seq[tuple[key, val: int]]
proc foo(s: var seq[tuple[key, val: int]]; i: int) =
s[i].key = 4*i
# r4 = addr(s[i])
# r4[0] = 4*i
proc bar() =
var s: T
s.f = newSeq[tuple[key, val: int]](3)
foo(s.f, 2)
echo s.f[2]
static:
bar()
|