4 ms·
Imagine if we had a Lisp with proper types, we could do something like... (typecase name (String (print name)) (otherwise (print "error: name must be
by tmtvl 2y ago
Imagine if we had a Lisp with proper types, we could do something like...
(typecase name
(String (print name))
(otherwise (print "error: name must be a string!")))
And if that Lisp had some kind of Object System, we may even go further and have stuff like...
(defmethod namecall ((name String))
(print name))
(defmethod namecall ((name Number))
(print "I am not a number, I am a free man!"))
- codr7 2y agoWild!
- behnamoh 2y agoclojure's multi-arity addresses this to some extent but pattern matching like we know it in Haskell is very weak in Lisps...
- p_l 2y agoIn case the sarcasm was missed, the latter code was actual common lisp code to pattern match a generic function, and the former is nearly-there when it comes to common lisp code
- klibertp 2y agoWhy "nearly"? Given how symbols are uppercased upon read in CL, 'string and 'String are actually the same symbol. I just checked: `(typecase "" (String 2) (otherwise 3))` returns 2, as expected. I'm less sure about the `defmethod`s, but I think they'll also work. Plus, the capitalized type name reminded me of Coalton[1], which is an internal DSL for CL that implements ML-style type system. There, the dispatch on a type (conventionally named by a capitalized symbol) happens statically. [1] https://github.com/coalton-lang/coalton https://github.com/coalton-lang/coalton
- p_l 2y agoBecause on the phone with one hand I couldn't check if I didn't forget some detail in typecase which I use way less often than defmethod :)
- klibertp 2y agoFair! :) The HyperSpec is full of easily forgettable details that almost never matter, until they do :D There are even undefined and implementation-defined things there, which can trigger C++ PTSD on a bad day...
- gjm11 2y agoIn case anyone is missing the snark here, all of tmtvl's code above is in fact perfectly valid Common Lisp.