diff options
| author | 2026-10-12 08:46:23 +0330 | |
|---|---|---|
| committer | 2026-10-12 08:46:23 +0330 | |
| commit | ecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch) | |
| tree | 70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /tests/programs/genmeth.zc | |
a
Diffstat (limited to 'tests/programs/genmeth.zc')
| -rw-r--r-- | tests/programs/genmeth.zc | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/programs/genmeth.zc b/tests/programs/genmeth.zc new file mode 100644 index 0000000..305c55c --- /dev/null +++ b/tests/programs/genmeth.zc @@ -0,0 +1,36 @@ +// methods on generic structs: each set of type arguments gets its own copy +struct pt { int x, y; }; +struct table<K, V> { + int n; K* keys; V* vals; + fn init(int cap) { + this.n = 0; + this.keys = new(K, cap) as K*; + this.vals = new(V, cap) as V*; + return 0; + } + fn put(K k, V v) { + this.keys[this.n] = k; + this.vals[this.n] = v; + this.n++; + return this.n; + } + fn width() { return sizeof(K) * 10 + sizeof(V); } + fn total() { + int s = 0; + for (int i = 0; i < this.n; i++) s += this.vals[i]; + return s; + } +}; +int main() { + table<int, int> a; + a.init(8); + a.put(1, 10); a.put(2, 20); a.put(3, 30); + put_n(a.n); put_c(' '); put_n(a.total()); put_c(' '); put_n(a.width()); put_c(' '); + table<pt, int>* b = new(table<pt, int>); + b.init(4); + put_n(b.width()); put_c(' '); + table<int, pt>* c = new(table<int, pt>); + c.init(4); + put_n(c.width()); put_c(10); + return 0; +} |
