-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstructtest.nt
72 lines (59 loc) · 1.14 KB
/
structtest.nt
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
module structtest;
macro import std.macro.assert;
extern(C) void* malloc(size_t);
extern(C) void free(void*);
void main() {
mut S s;
s.a = 3;
int* ap = &s.a;
assert(*ap == 3);
void* mem = malloc(8);
S* sp = cast(S*) mem;
int* ip = cast(int*) mem;
sp.a = 5;
sp.b = 8;
assert(sp.a == 5);
assert(*ip == 5);
(*sp).a = 6;
assert(sp[0].a == 6);
S t = S(2, 3);
assert(t.a == 2);
assert(t.b == 3);
assert(t.sum() == 5);
free(mem);
mut int count;
S test() { count += 1; return S(2, 3); }
assert(test.sum == 5);
assert(count == 1);
assert(S.init.sum == 5);
assert((new A).test.i == 5);
assert(S(3).sum == 8);
assert(T(2, 3).((a, b)) == (3, 2));
assert(T(3).((a, b)) == (5, 3));
}
struct S
{
int a;
int b = 5;
int sum() { return a + b; }
static S init() { return S(2, 3); }
}
struct T
{
int a;
int b;
this(this.b, this.a = 5) {}
}
class A
{
this() { }
struct X { int i; }
X test() {
struct Y {
int i;
int get() { return i; }
}
Y y = Y(5);
return X(y.get);
}
}