diff --git a/core/ControlMacros.carp b/core/ControlMacros.carp index fed5d864..47460d38 100644 --- a/core/ControlMacros.carp +++ b/core/ControlMacros.carp @@ -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))) diff --git a/core/Macros.carp b/core/Macros.carp index fb9a0fd1..b96f83a9 100644 --- a/core/Macros.carp +++ b/core/Macros.carp @@ -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`." diff --git a/test/control_macros.carp b/test/control_macros.carp new file mode 100644 index 00000000..0efbaf42 --- /dev/null +++ b/test/control_macros.carp @@ -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") +)