(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// Create Interface interface Shape = { func area = (this) -> F32; func num_sides = () -> UInt { return 0; } // method with default } type Quad = struct { var width: F32; var height: F32; func area = (this) -> F32 { return this.width * this.height; } func num_sides = () -> UInt { return 4; } // implementation of Shape for Quad impl Shape{ num_sides = num_sides, area = area, } } type Circle = struct { var radius: F32; func get_area = (this) -> F32 { return 3.14 * this.radius * this.radius; } func get_num_sides = () -> UInt { return 0; } // Implementation of Shape for Circle impl Shape{ num_sides = get_num_sides, // method names don't have to be the same // using default for Shape.area } } // this function becomes a template on the shape passed to it func get_shape_num_sides = (shape: Shape) -> UInt { return shape.num_sides(); } // runtime polymorphism // `shape` is a struct of a pointer to the specific shape and a pointer to a `vtable` func get_shape_area = (shape: Shape*) -> UInt { return shape.area(); } func entry = () #entry -> UI8 { const quad = new Quad{ width = 1.0, height = 2.0, }; const circle = new Circle{ radius = 2.0, }; const num_sides_of_quad: UInt = get_shape_num_sides(quad); const area_of_circle: F32 = get_shape_area(&circle as Shape*); return 0; }