Skip to content

Commit

Permalink
Merge pull request #169 from kilianmh/feat/typecase-let
Browse files Browse the repository at this point in the history
Feat: add typecase-let, etypecase-let
  • Loading branch information
ruricolist committed Apr 23, 2024
2 parents 0fd07b1 + 4d39bb8 commit d1afae8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions control-flow.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ Burson."
(ecase ,var
,@cases)))

(defmacro typecase-let ((var expr) &body cases)
"Like (let ((VAR EXPR)) (typecase VAR ...)), with VAR read-only."
`(let1 ,var ,expr
(typecase ,var
,@cases)))

(defmacro etypecase-let ((var expr) &body cases)
"Like (let ((VAR EXPR)) (etypecase VAR ...)), with VAR read-only."
`(let1 ,var ,expr
(etypecase ,var
,@cases)))

(defmacro comment (&body body)
"A macro that ignores its body and does nothing. Useful for
comments-by-example.
Expand Down
2 changes: 2 additions & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
#:ecase-let
#:cond-let
#:case-let
#:typecase-let
#:etypecase-let
#:bcond
#:comment
#:example
Expand Down
21 changes: 21 additions & 0 deletions tests/control-flow.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,27 @@
(ecase-let (x 17)
(0 x) (1 (1+ x)))))

(test typecase-let
(is (eql 2
(typecase-let (x 2)
(string 20)
(integer x))))
(is (string= "test-here"
(typecase-let (y "test")
(integer "not")
(string (concatenate 'string y "-here"))))))

(test etypecase-let
(is (eql 2
(etypecase-let (x 'asdf)
(string 20)
(integer x)
(symbol 2))))
(signals type-error
(etypecase-let (y 'test-symbol)
(integer "not")
(string (concatenate 'string y "-here")))))

(test comment
(is-false (comment "This is a comment")))

Expand Down

0 comments on commit d1afae8

Please sign in to comment.