forked from alex-eg/sex
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.
48 lines
1.4 KiB
Scheme
48 lines
1.4 KiB
Scheme
;;; Uses the greet module. Build both, then link them:
|
|
;;;
|
|
;;; ./sexc example/greet.sex -c -o greet.o
|
|
;;; ./sexc example/greet-app.sex -o greet-app -- greet.o
|
|
;;;
|
|
;;; `(import greet)' pastes greet's public declarations here: `greet'
|
|
;;; as a prototype and `greet-count' as an extern. Both keep external
|
|
;;; linkage, so they refer to the one definition in greet.o rather than
|
|
;;; to private copies.
|
|
;;;
|
|
;;; Also check that import populates type-database, by means of
|
|
;;; describe-fields macro, which should work on imported type.
|
|
|
|
(include stdio.h)
|
|
|
|
(import greet)
|
|
|
|
(pub fn main () int
|
|
(greet "world")
|
|
(greet "sex")
|
|
(printf "%d greetings\n" greet-count)
|
|
(describe-fields greeting)
|
|
(printf "\n")
|
|
;; ...and through the imported typedef for it
|
|
(describe-fields greeting-t)
|
|
(printf "\n")
|
|
(var m (enum mood) grumpy)
|
|
(printf "mood %d\n" m)
|
|
|
|
(var add-10 (closure ((int)) int) (make-adder 10))
|
|
(printf "closure %d %d" (add-10 5) (apply-twice add-10 1))
|
|
(var base int 100)
|
|
(var here (closure ((int)) int)
|
|
(closure ((x int)) int (base) (return (+ base x))))
|
|
(printf " %d\n" (apply-twice here 1))
|
|
|
|
;; the bare record name, then the method both ways round
|
|
(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))
|
|
((none ()) (printf "colour none\n")))
|
|
(return 0))
|