Data (type-)checking utilities? Artyom Bologov (14 Jul 2024 21:06 UTC)
Re: Data (type-)checking utilities? Arthur A. Gleckler (14 Jul 2024 21:11 UTC)
Re: Data (type-)checking utilities? Artyom Bologov (14 Jul 2024 21:27 UTC)
Re: Data (type-)checking utilities? Daphne Preston-Kendal (17 Jul 2024 09:23 UTC)
Re: Data (type-)checking utilities? Maxim (18 Jul 2024 12:52 UTC)
Re: Data (type-)checking utilities? Artyom Bologov (19 Jul 2024 17:17 UTC)

Re: Data (type-)checking utilities? Artyom Bologov 14 Jul 2024 21:27 UTC

Hi Arthur,

> Editor here.  I would welcome a SRFI proposing a well-thought-out collection of type-checking primitives.  Would you be willing to share
> a few examples to give people on this mailing list a preview?

Sure! So what one often needs with regards to type checking is:
(1) Checking the value(s) returned by the form.
(2) Checking function arguments.
(3) Dispatching the behavior over types.

(1) might be achieved by something like Common Lisp THE operator. I'm
naming my macro achieving the same goal RETURN-CHECKED for no particular
reason:

(return-checked real? (sqrt 2))
;; => 1.4142135623730951

(2) might be approached from two sides. The first is checking arguments
one-by-one in the function body:

(define (foo a b c)
  (check-arg integer? a 'foo)
  (check-arg integer? b 'foo)
  ;; Or even (return-checked integer? (if c a b))
  (if c a b))

the second option is integrating the checking into function definition,
mimicking statically typed languages:

(define-checked (foo (a integer?) (b integer?) c)
  (if c a b))

(3) is achieved by something like Common Lisp typecase, here called
check-case:

(define (->logint x)
  (check-case
   x
   (integer? x)
   (boolean? (if x 1 0))))

There are more utils in the works, but these are the core of what I'm
thinking about.

Thanks,
--
Artyom Bologov