Something went wrong. Try again.
Jam Programming Language jamlang.org
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980// Single test: instance method call on generic struct.const { assert } = import("test");
fn Holder(T: type) type { return struct { value: T,
fn get(self: Self) T { return self.value; } };}
const HolderI32 = Holder(i32);
tfn instanceMethod() { var h: HolderI32 = HolderI32 { value: 7 }; assert(h.get(), 7);}
fn Holder2(T: type) type { return struct { value: T,
fn get(self: Self) T { return self.value; }
fn getTwice(self: Self) T { // self.get() from inside another method body return self.get(); } };}
const Holder2I32 = Holder2(i32);
tfn selfMethodCallFromBody() { var h: Holder2I32 = Holder2I32 { value: 11 }; assert(h.getTwice(), 11);}
fn Accumulator(T: type) type { return struct { sum: T,
fn add(self: mut Self, x: T) { self.sum = self.sum + x; }
fn addTwice(self: mut Self, x: T) { // mut self method calls another mut self method via // `self.add(x)`. Receiver is auto-addressed (since add // takes `mut self`), so the synthesis wraps self in // AddressOf. self.add(x); self.add(x); }
fn total(self: Self) T { return self.sum; } };}
const AccumI32 = Accumulator(i32);
tfn mutInstanceMethod() { var a: AccumI32 = AccumI32 { sum: 0 }; a.add(3); a.add(5); assert(a.total(), 8);}
tfn selfMutMethodFromMutBody() { var a: AccumI32 = AccumI32 { sum: 0 }; a.addTwice(7); assert(a.total(), 14);}