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))
(parameter (spec-parameter spec)))
(check-realization form name type parameter wanted given)
(fold (lambda (entry acc)
(process-realized-method form name type
(and pub? (cdr entry)) (car entry) acc))
acc
(realization-methods type parameter wanted given))))))
;; every method is registered before any body is walked, so they
;; may call one another in any order, as an ops block's may; one
;; called above its definition is declared ahead of them all
(let* ((methods (map (lambda (entry)
(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
;;; 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
;;; not carry `pub' of their own, the way an ops block's do, because
;;; the spec already says which of them there are.
(define (process-realized-method form spec type pub? method acc)
;;; the spec already says which of them there are. The method's fn
;;; 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))
(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)
(sex-error form "method is already defined on" type name))
(add-method! type name qualified)
(process-fn (copy-form-source! form (if pub? (cons 'pub renamed) renamed))
acc)))
(process-fn (sex-fn-prototype fn) (list))
fn))
;;; T in a spec's method stands for whatever realizes it
(define (substitute-parameter parameter type form)

View File

@@ -2,7 +2,8 @@
(output "$12.34"
"(3, 4)"
"max 1234"
"realizes ord: money 1, point 0")
"realizes ord: money 1, point 0"
"calls its own: 9")
(return 0)
;;; 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))
;;; 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
(var m money #(money : 1234))
(var p point #(point : 3 4))
@@ -65,4 +79,5 @@
(printf "max %d\n" (. big cents))
(printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point))
(printf "calls its own: %d\n" (.total p))
(return 0))