mirror of
https://github.com/carp-lang/Carp.git
synced 2024-08-16 00:30:24 +03:00
feat: Add additional ignore macros (#1300)
* feat: Add additional ignore macros This commit adds two new ignore macros, ignore*, which wraps an arbitrary number of forms in calls to `ignore` and ignore-do, which wraps an arbitrary number of forms in ignore, then bundles the whole in a do call, effectively executing each form only for side effects and ignoring all results. * docs: Update ignore* docs Link to `ignore` doc Co-authored-by: Veit Heller <veit@veitheller.de> * fix: Call ignore* in ignore-do ignore-all was an old name that no longer exists! * test: Add test for ignore-do Co-authored-by: Veit Heller <veit@veitheller.de>
This commit is contained in:
parent
0c9c475e6c
commit
c9967ddf6e
@ -133,6 +133,13 @@ Example:
|
||||
(defmacro forever-do [:rest forms]
|
||||
(list 'while true (cons 'do forms)))
|
||||
|
||||
(doc ignore-do
|
||||
("Wraps side-effecting `forms` in a `do`, " false)
|
||||
"ignoring all of their results."
|
||||
"In other words, executes `forms` only for their side effects.")
|
||||
(defmacro ignore-do [:rest forms]
|
||||
(cons 'do (expand (apply ignore* forms))))
|
||||
|
||||
(doc when "is an `if` without an else branch.")
|
||||
(defmacro when [condition form]
|
||||
(list 'if condition form (list)))
|
||||
|
@ -258,6 +258,10 @@ the filename and module name are the same.")
|
||||
(defmacro ignore [form]
|
||||
(list 'let (array '_ form) (list)))
|
||||
|
||||
(doc ignore* "Wraps all forms passed as an argument in a call to [`ignore`](#ignore).")
|
||||
(defmacro ignore* [:rest forms]
|
||||
(map (fn [x] (cons-last x (list 'ignore))) forms))
|
||||
|
||||
(doc const-assert
|
||||
"Asserts that the expression `expr` is true at compile time."
|
||||
"Otherwise it will fail with the message `msg`."
|
||||
|
15
test/control_macros.carp
Normal file
15
test/control_macros.carp
Normal file
@ -0,0 +1,15 @@
|
||||
(load "Test.carp")
|
||||
(use Test)
|
||||
|
||||
(def test-string @"")
|
||||
|
||||
(defn test-ignore-do []
|
||||
(ignore-do (+ 2 2) ;; ignored
|
||||
(set! test-string @"new-string") ;; ignored, but side-effect performed
|
||||
(- 4 4)))
|
||||
|
||||
(deftest test
|
||||
(assert-true test
|
||||
(and (= () (test-ignore-do)) (= &test-string "new-string"))
|
||||
"ignore-do performs side effects and ignores all results")
|
||||
)
|
Loading…
Reference in New Issue
Block a user