[CM] Share the define-macro implementation of typed-define

Da Shen da at liii.pro
Tue Jan 7 00:10:52 PST 2025


Inspired by typed-lambda in S7 Scheme stuff.scm
I just implemented typed-define. It is licensed in Apache License by the Goldfish Scheme authors and it is ok for the author to adopt it and re-distribute it in 0 clause BSD license.
Here is the code snippets ( it is based on define*, and predicates-based type checking is mandatory ):
(define-macro (typed-define name-and-params x . xs)
 (let* ((name (car name-and-params))
 (params (cdr name-and-params)))
 `(define* (,name ,@(map (lambda (param)
 (let ((param-name (car param))
 (type-pred (cadr param))
 (default-value (cddr param)))
 (if (null? default-value)
 param-name
 `(,param-name ,(car default-value)))))
 params))
 ,@(map (lambda (param)
 (let ((param-name (car param))
 (type-pred (cadr param)))
 `(unless (,type-pred ,param-name)
 (error 'type-error (string-append "Invalid type for " (symbol->string ',param-name))))))
 params)
 ,x
 , at xs)))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20250107/57a1e1da/attachment.html>


More information about the Cmdist mailing list