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

@@ -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))