This was question here earlier. I’m copying here for hopefully a more in-depth discussion.
In Common Lisp I can write a lambda form like the following:
(lambda (a b c d) (declare (type number a c) (type integer c) (type string d)) ...)
And depending on which compiler I’m using, the compiler is allowed to optimize the code in specific ways.
I can also write a macro to implement my own version of lambda such as
(my-lambda (a b c d) (declare (type number a c) (type integer c) (type string d)) ...)
and in the macro, examine the declarations (as they are just raw s-expressions given to the macro as input). The advantage being that every CL user automatically knows how to use my-lambda because I’m promising that it has the same syntax as lambda.
QUESTION: is there any sort of type optional type declaration which I can use in my macro which corresponds to something the language already supports? I don’t want to invent my own if there is already one.
Here is what I have so far. I wonder if what syntax a clojure programmer would expect for the variable type correspondences.
(destructuring-case (some-function-call) [a [b c] & d] (a Boolean b String d Boolean) (println [:first a b c d]) [a b] (a Boolean b (or String Boolean)) (println [:second a b]))
The meaning is that if
(some-function-call) returns a sequence matching the template
[a [b c] & d] where
a is a
b is a
d (which is guaranteed to be a sequence) is a sequence of zero or more
(println [:first a b c]) with the appropriate elements bound. Otherwise if the sequence matches
[a b] where
String or a
(println [:second a b]).