Files use .rex, the command is rex, and the crate, runtime prefixes, docs, editor files and examples all use the new name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
1.6 KiB
Rexx
80 lines
1.6 KiB
Rexx
interface Shape {
|
|
area(): float
|
|
name(): string
|
|
scale(k: float)
|
|
}
|
|
|
|
struct Circle {
|
|
r: float
|
|
func area(): float { return 3.0 * r * r }
|
|
func name(): string { return "circle" }
|
|
func scale(k: float) { r = r * k }
|
|
}
|
|
|
|
struct Rect {
|
|
w, h: float
|
|
tag: string
|
|
func area(): float { return w * h }
|
|
func name(): string { return "rect " + tag }
|
|
func scale(k: float) {
|
|
w = w * k
|
|
h = h * k
|
|
}
|
|
}
|
|
|
|
enum Dot {
|
|
Small
|
|
Big
|
|
func area(): float { return 1.0 }
|
|
func name(): string { return "dot" }
|
|
func scale(k: float) {}
|
|
}
|
|
|
|
struct Scene {
|
|
shapes: []Shape
|
|
main_shape: Shape
|
|
}
|
|
|
|
func describe(s: Shape): string {
|
|
return "{s.name()}: {s.area()}"
|
|
}
|
|
|
|
func biggest(shapes: []Shape): Shape {
|
|
var best = shapes[0]
|
|
for s in shapes {
|
|
if s.area() > best.area() { best = s }
|
|
}
|
|
return best
|
|
}
|
|
|
|
func total[T: Shape](xs: []T): float {
|
|
var sum = 0.0
|
|
for x in xs { sum += x.area() }
|
|
return sum
|
|
}
|
|
|
|
func main() {
|
|
var shapes: []Shape = [Circle{1.0}, Rect{2.0, 3.0, "a"}, Dot.Big]
|
|
shapes.push(Circle{2.0})
|
|
for s in shapes { print(describe(s)) }
|
|
print(biggest(shapes).name())
|
|
|
|
// interface values are copies: changing one doesn't change the original
|
|
var c = Circle{1.0}
|
|
var s: Shape = c
|
|
s.scale(10.0)
|
|
print(c.r, s.area())
|
|
var t = s
|
|
t.scale(2.0)
|
|
print(s.area(), t.area())
|
|
|
|
var scene = Scene{shapes, Rect{1.0, 1.0, "main"}}
|
|
scene.main_shape.scale(3.0)
|
|
print(scene.main_shape.area(), scene.shapes.len())
|
|
print(scene.main_shape)
|
|
|
|
var none: Shape
|
|
print(none == nil, s == nil)
|
|
print(total(shapes))
|
|
}
|