put the method in the head, not in a field position

(. v length) made the shape of a form depend on the receiver's type:
(. a b c d e) was either the path a.b.c.d.e or a/b called on c d e,
and nothing but the ops registry could say which. Resolution had to
arbitrate, a field won, and so adding a field named length to vec2
turned every (. v length) in reach from a call into a read -- silently,
and only sometimes into a type error.

Resolution may need types; shape must not. (.length v) puts the method
where an operator belongs, so (. v x) is always the field x, a field
and a method of one name coexist, and a reader needs no symbol table
to see where the arguments start.

Which of &, * or nothing goes in front of the receiver now follows
from its own type against the method's self, rather than from whether
the caller wrote . or ->, so (.length v) and (.length p) are the same
call and -> is member access again and nothing else.

The reader already allowed it: only a standalone . becomes dot-access,
and .foo is the ordinary symbol designated initializers are made of.
This commit is contained in:
2026-10-03 23:17:44 +03:00
parent 3a10ee099b
commit bbdedb9777
3 changed files with 74 additions and 54 deletions

View File

@@ -36,7 +36,7 @@
;; the bare record name, then the method both ways round
(var v vec2 #(vec2 : 3 4))
(printf "vec2 %d %d\n" (. v manhattan) (vec2/manhattan (& v)))
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
(var c colour (colour/rgb 1 2 3))
(match c

View File

@@ -5,9 +5,14 @@
(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
;;; reachable both ways: spelled out as vec2/length, or as (.length v).
;;; The method sits in the head, not in a field position, so (. v x) is
;;; always the field x and the two cannot be confused -- here vec2 has
;;; both a field x and methods, and nothing arbitrates between them.
;;;
;;; Whether the receiver is addressed, dereferenced or passed along is
;;; decided by its type against the method's own, so (.length v) and
;;; (.length p) both work. `ops' takes a plain C struct too: it keys on
;;; the tag, not on the typedef a record happens to add.
(include stdio.h)
@@ -17,7 +22,7 @@
(ops vec2
(pub fn length ((self (* const vec2))) float
"Pointer self, so (. v length) has to take the address."
"Pointer self, so (.length v) has to take the address."
(return (sqrtf (+ (* (-> self x) (-> self x))
(* (-> self y) (-> self y))))))
@@ -26,7 +31,7 @@
(*= (-> self y) k))
(fn sum-of ((self vec2)) float
;; value self, so (-> p sum-of) has to dereference
;; value self, so (.sum-of p) has to dereference
(return (+ (. self x) (. self y)))))
(struct raw ((a int) (b int)))
@@ -39,13 +44,13 @@
(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 " (.length v) (vec2/length (& v)))
(.scale v 2.0)
(printf "%.1f %.1f %.1f %.1f\n"
(-> p length) (. v sum-of) (-> p sum-of) (. v x))
(.length p) (.sum-of v) (.sum-of p) (. v x))
(var r (struct raw) #(struct raw : 6 8))
(printf "struct receiver: %d\n" (. r total))
(printf "struct receiver: %d\n" (.total r))
(var u vec2 #(vec2 : 8.0 0.0))
(printf "private: %.1f\n" (vec2/sum-of u))