(TODO)
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 100type Vec2 = struct { var x: F32; // member variable var y: F32; func multiplyBy = (this mut, coefficient: F32) -> Void { // method this.x *= coefficient; this.y *= coefficient; } func createZero = () -> Vec2 { // member function (no `this`) return new Vec2{x = 0.0, y = 0.0}; } func + = (this, rhs: Vec2) -> Vec2 { // operator overloading return new Vec2{ x = this.x + rhs.x, y = this.y + rhs.y, }; } func * = (this, coefficient: F32) -> (output: Vec2) { // arg of operator overloading can be a different type output = copy this; output.multiplyBy(coefficient); return...; } } type NonTrivialNum = struct { func new = (num: Int) #rt -> (output: NonTrivialNum) { // initialization new output.num = copy num; NonTrivialNum.num_existing += 1; return...; } func new = () #rt -> (output: NonTrivialNum) { // default initialization new output.num = 0; NonTrivialNum.num_existing += 1; return...; } func new = (this mut, num: Int) #rt -> Void { // assignment new this.num = copy num; } func delete = (this) #rt -> Void { // delete if(this.num != null){ NonTrivialNum.num_existing -= 1; } } func copy = (this) #rt -> (output: NonTrivialNum) { // initialization copy output.num = copy this.num; NonTrivialNum.num_existing += 1; return...; } func copy = (this, target: NonTrivialNum mut) #rt -> Void { // assignment copy target.num = copy this.num; NonTrivialNum.num_existing += 1; } func move = (this mut) #rt -> (output: NonTrivialNum) { // initialization move output.num = this.num.extract(); return...; } func move = (this mut, target: NonTrivialNum mut) #rt -> Void { // assignment move target.num = this.num.extract(); NonTrivialNum.num_existing -= 1; } // private, cannot be accessed outside this struct var num: Int? #priv; // Demo explanaition: null if `this` was moved from var num_existing: UInt #priv #global = 0; func getNumExisting = () #rt -> UInt { return copy NonTrivialNum.num_existing; } } func entry = () #entry -> UI8 { var foo = new NonTrivialNum(); // 1 NonTrivialNum exists var bar: NonTrivialNum = copy foo; // 2 NonTrivialNum exists bar = move foo; // 1 NonTrivialNum exists return NonTrivialNum.getNumExisting() as UI8; // returns 1 }