let a realization's methods call one another

Each was registered as its body was walked, so a method could not call
one coming after it -- a default included, defaults coming after the
written ones. All are registered first now, as an ops block's are.
This commit is contained in:
2026-10-18 22:02:55 +03:00
parent 94f41b5fad
commit cb37821e3e
2 changed files with 39 additions and 11 deletions

View File

@@ -340,11 +340,21 @@
(let ((wanted (spec-methods spec)) (let ((wanted (spec-methods spec))
(parameter (spec-parameter spec))) (parameter (spec-parameter spec)))
(check-realization form name type parameter wanted given) (check-realization form name type parameter wanted given)
(fold (lambda (entry acc) ;; every method is registered before any body is walked, so they
(process-realized-method form name type ;; may call one another in any order, as an ops block's may; one
(and pub? (cdr entry)) (car entry) acc)) ;; called above its definition is declared ahead of them all
acc (let* ((methods (map (lambda (entry)
(realization-methods type parameter wanted given)))))) (register-realized-method form name type
(and pub? (cdr entry))
(car entry)))
(realization-methods type parameter wanted given)))
(bodies (map (lambda (m) (process-fn m (list))) methods))
(early (filter (lambda (m) (called-before? m methods bodies))
methods)))
(append (append-map identity (reverse bodies))
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc))
acc
early)))))))
;;; What is written here, which a pub realization exports, and then ;;; What is written here, which a pub realization exports, and then
;;; every default nobody overrode, which it does not: a default is ;;; every default nobody overrode, which it does not: a default is
@@ -396,16 +406,19 @@
;;; A realization is public or private as a unit -- its methods do ;;; A realization is public or private as a unit -- its methods do
;;; not carry `pub' of their own, the way an ops block's do, because ;;; not carry `pub' of their own, the way an ops block's do, because
;;; the spec already says which of them there are. ;;; the spec already says which of them there are. The method's fn
(define (process-realized-method form spec type pub? method acc) ;;; form, renamed to pointːsizedːtotal, once it is registered under
;;; `total' and its signature known.
(define (register-realized-method form spec type pub? method)
(let* ((name (second method)) (let* ((name (second method))
(qualified (qualify-op (qualify-op type spec) name)) (qualified (qualify-op (qualify-op type spec) name))
(renamed (rename-fn method qualified))) (renamed (copy-form-source! form (rename-fn method qualified)))
(fn (if pub? (copy-form-source! form (cons 'pub renamed)) renamed)))
(when (get-method type name) (when (get-method type name)
(sex-error form "method is already defined on" type name)) (sex-error form "method is already defined on" type name))
(add-method! type name qualified) (add-method! type name qualified)
(process-fn (copy-form-source! form (if pub? (cons 'pub renamed) renamed)) (process-fn (sex-fn-prototype fn) (list))
acc))) fn))
;;; T in a spec's method stands for whatever realizes it ;;; T in a spec's method stands for whatever realizes it
(define (substitute-parameter parameter type form) (define (substitute-parameter parameter type form)

View File

@@ -2,7 +2,8 @@
(output "$12.34" (output "$12.34"
"(3, 4)" "(3, 4)"
"max 1234" "max 1234"
"realizes ord: money 1, point 0") "realizes ord: money 1, point 0"
"calls its own: 9")
(return 0) (return 0)
;;; A spec is a contract and emits nothing; a realization is the ;;; A spec is a contract and emits nothing; a realization is the
@@ -53,6 +54,19 @@
(defmacro (ord? type) (if (realizes? 'ord type) 1 0)) (defmacro (ord? type) (if (realizes? 'ord type) 1 0))
;;; A realization's methods may call one another, whatever order they
;;; come in: `total', written, calls the default `doubled', which calls
;;; the later default `plus-one', which calls `base', written below them
(spec (sized T)
(fn total ((self (* const T))) int)
(fn doubled ((self (* const T))) int (return (* 2 (.plus-one self))))
(fn plus-one ((self (* const T))) int (return (+ 1 (.base self))))
(fn base ((self (* const T))) int))
(realize (sized point)
(fn total ((self (* const point))) int (return (+ 1 (.doubled self))))
(fn base ((self (* const point))) int (return (. self x))))
(pub fn main () int (pub fn main () int
(var m money #(money : 1234)) (var m money #(money : 1234))
(var p point #(point : 3 4)) (var p point #(point : 3 4))
@@ -65,4 +79,5 @@
(printf "max %d\n" (. big cents)) (printf "max %d\n" (. big cents))
(printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point)) (printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point))
(printf "calls its own: %d\n" (.total p))
(return 0)) (return 0))