forked from alex-eg/sex
implement spec and realize
A spec is a contract that emits nothing; a realization is that contract's methods for one type, mangled typeːspecːmethod. A default is copied into every realizing type with the parameter substituted, so the `show' a default calls is that type's own and the whole thing stays static dispatch with nothing left at runtime. The methods land in the ops registry beside inherent ones, which is what lets (.show v) find them with no second lookup -- and makes the duplicate check already there catch a spec method colliding with an ops method or with another spec's. Coherence came free. A realization's signatures are checked against the spec, arguments and return type both, after substituting the parameter; a missing required method is named, and so is one the spec has not got. That is what makes a spec a contract rather than a naming convention. The bound is asked rather than declared: realizes? goes into the macro API beside type-of, and a generic calls it and raises its own error. That is the return this document was written for -- `plain does not realize ord' where the line is, instead of a wall of C errors from a monomorphization two stages downstream. A default is static even when the realization is pub, and does not travel on export: every importing unit synthesizes its own copy, so sending one too would be a second definition of one symbol. The cost is that a module whose defaults call printf has to pub include stdio.h -- which is also why pub include and pub import now work, both having been accepted by read-public-interface and refused by semen.
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
|
||||
SEXC ?= ../../sexc
|
||||
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\ncolour 123
|
||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ncolour 123
|
||||
|
||||
check:
|
||||
@$(SEXC) greet.sex -c -o greet.o
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
(var v vec2 #(vec2 : 3 4))
|
||||
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
||||
|
||||
(.show-line v)
|
||||
|
||||
(var c colour (colour/rgb 1 2 3))
|
||||
(match c
|
||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
;;; the compiled object still has to be handed to the C compiler. See
|
||||
;;; greet-app.sex.
|
||||
|
||||
(include stdio.h)
|
||||
(pub include stdio.h)
|
||||
|
||||
(pub var greet-count ;; a comment in the header of a public form is
|
||||
;; not part of it: what the importer is given has
|
||||
@@ -65,6 +65,20 @@
|
||||
;;; static: two units defining colourːrgb would not link.
|
||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||
|
||||
;;; A spec and a realization over the boundary. The written method
|
||||
;;; travels as a prototype and keeps external linkage; the default
|
||||
;;; does not travel at all -- the importer synthesizes its own static
|
||||
;;; copy from the spec, which is why a default is static even here.
|
||||
(pub spec (printable T)
|
||||
(fn show ((self (* const T))) void)
|
||||
(fn show-line ((self (* const T))) void
|
||||
(.show self)
|
||||
(printf "\n")))
|
||||
|
||||
(pub realize (printable vec2)
|
||||
(fn show ((self (* const vec2))) void
|
||||
(printf "vec2(%d %d)" (-> self x) (-> self y))))
|
||||
|
||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||
(fn unused-helper () void
|
||||
(printf "private\n"))
|
||||
|
||||
Reference in New Issue
Block a user