Reputation: 73
I'm new in clojure so I would like to know some things
There are 2 ways to typehint your code, I would like to know the difference
^String and others using #^String
When I saw the code from when I was a little disappointed, it's an if if that is not true could explain me why (assert is a when with throw)
(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
{:added "1.0"}
[test & body]
(list 'if test (cons 'do body)))
Upvotes: 2
Views: 87
Reputation: 45736
#^
is the old, deprecated syntax. Just use ^
now going forward, unless you're running a very old version of Clojure (pre-1.2).
Why be disappointed? This is part of the beauty of languages like Clojure! when
is just defined in terms of if
. It looks like a language construct, but it's actually just a macro that anyone can write. when
literally turns into an if
with a do
at compile time. It would arguably complicate the language to have made when
a special form when it's entirely possible to define it in terms of existing forms.
The purpose of when
is to carry out side effects when the condition holds, but do nothing otherwise. It's simply a readability helper. It's similar to the if-not
, and when-not
macros that just turn into if
s with their condition negated. They aren't necessary by any means, but they certainly help clean up code.
Upvotes: 7