(input) (output "5.0 5.0 10.0 14.0 14.0 6.0" "struct receiver: 14" "private: 8.0") (return 0) ;;; An ops block is mangled fns plus a registry entry, so a method is ;;; reachable both ways: spelled out as vec2/length, or through the ;;; receiver as (. v length). A field still wins over a method of the ;;; same name, and `ops' works on a plain C struct too -- it keys on ;;; the tag, not on the typedef a record happens to add. (include stdio.h) (include math.h) (record vec2 ((x float) (y float))) (ops vec2 (pub fn length ((self (* const vec2))) float "Pointer self, so (. v length) has to take the address." (return (sqrtf (+ (* (-> self x) (-> self x)) (* (-> self y) (-> self y)))))) (fn scale ((self (* vec2)) (k float)) void (*= (-> self x) k) (*= (-> self y) k)) (fn sum-of ((self vec2)) float ;; value self, so (-> p sum-of) has to dereference (return (+ (. self x) (. self y))))) (struct raw ((a int) (b int))) (ops raw (fn total ((self (* const (struct raw)))) int (return (+ (-> self a) (-> self b))))) (pub fn main () int (var v vec2 #(vec2 : 3.0 4.0)) (var p (* vec2) (& v)) (printf "%.1f %.1f " (. v length) (vec2/length (& v))) (. v scale 2.0) (printf "%.1f %.1f %.1f %.1f\n" (-> p length) (. v sum-of) (-> p sum-of) (. v x)) (var r (struct raw) #(struct raw : 6 8)) (printf "struct receiver: %d\n" (. r total)) (var u vec2 #(vec2 : 8.0 0.0)) (printf "private: %.1f\n" (vec2/sum-of u)) (return 0))