aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/genmeth.zc
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /tests/programs/genmeth.zc
a
Diffstat (limited to 'tests/programs/genmeth.zc')
-rw-r--r--tests/programs/genmeth.zc36
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;
+}