Clojure, has a declare
macro that allows you to forward-declare functions or variables. It seems to function exactly as def
: Both (declare x)
and (def x)
create #<Unbound Unbound: #'user/x>
When should (declare x)
be used instead of (def x)
?
As the name itself implies, forward declaration is just a Declaration and not a definition. So, you will declare saying the compiler that it is a class and I just declaring it here and will provide you the definition when am gonna use it. So, normally you forward declare in the Header file and #include in the .
You will usually want to use forward declaration in a classes header file when you want to use the other type (class) as a member of the class. You can not use the forward-declared classes methods in the header file because C++ does not know the definition of that class at that point yet.
A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.
forward declaration (countable and uncountable, plural forward declarations) (programming) The declaration of an identifier before it is given a complete definition, so that the compiler can know its data type and memory size.
Both declare
and def
do create an unbound var, but there are 3 advantages to using declare
:
(declare x y z)
{:declared true}
declare
is arguably more clear and idiomatic(source declare)
:
(defmacro declare "defs the supplied var names with no bindings, useful for making forward declarations." {:added "1.0"} [& names] `(do ~@(map #(list 'def (vary-meta % assoc :declared true)) names)))
The documentation gives the answer:
=> (doc declare) ------------------------- clojure.core/declare ([& names]) Macro defs the supplied var names with no bindings, useful for making forward declarations.
Looking at the implementation, it's clear that declare
is defined in terms of def
and provides a little bit of syntax sugar. So functionally, they're pretty much the same.
The advantage of declare
is to show intent for a later reader. (declare x y z)
means I intended to make a forward declaration of those symbols, because the macro is useful for making forward declarations.
(def x) (def y) (def z)
means I'm interning these symbols, but you don't know if I meant to give them definitions and forgot to or whether I'm making forward declarations, or maybe something else subtle.
So, (declare x)
should be preferred over (def x)
when you're making a forward declaration, to show mercy on future readers of your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With