Last active
August 30, 2017 11:59
-
-
Save skelter/5091143 to your computer and use it in GitHub Desktop.
defrecord is one of my favorite examples of the usefulness of macros in Clojure. This particular use demonstrates adding some mild syntax and generating a lot of boiler plate. Let's see what all it does.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4837 ◯ lein new macrofun | |
Generating a project called macrofun based on the 'default' template. | |
To see other templates (app, lein plugin, etc), try `lein help new`. | |
hobiecat ओम् /tmp: | |
4838 ◯ cd macrofun | |
hobiecat ओम् /tmp/macrofun: | |
4839 ◯ lein repl | |
nREPL server started on port 48544 | |
REPL-y 0.1.0-beta10 | |
Clojure 1.4.0 | |
Exit: Control+D or (exit) or (quit) | |
Commands: (user/help) | |
Docs: (doc function-name-here) | |
(find-doc "part-of-name-here") | |
Source: (source function-name-here) | |
(user/sourcery function-name-here) | |
Javadoc: (javadoc java-object-or-class-here) | |
Examples from clojuredocs.org: [clojuredocs or cdoc] | |
(user/clojuredocs name-here) | |
(user/clojuredocs "ns-here" "name-here") | |
user=> (pprint (macroexpand-1 '(defrecord Person [name email address phone]))) | |
(clojure.core/let | |
[] | |
(clojure.core/declare ->Person) | |
(clojure.core/declare map->Person) | |
(deftype* | |
Person | |
user.Person | |
[name email address phone __meta __extmap] | |
:implements | |
[clojure.lang.IRecord | |
clojure.lang.IObj | |
clojure.lang.ILookup | |
clojure.lang.IKeywordLookup | |
clojure.lang.IPersistentMap | |
java.util.Map | |
java.io.Serializable] | |
(clojure.core/entrySet | |
[this__5608__auto__] | |
(clojure.core/set this__5608__auto__)) | |
(clojure.core/values | |
[this__5607__auto__] | |
(clojure.core/vals this__5607__auto__)) | |
(clojure.core/keySet | |
[this__5606__auto__] | |
(clojure.core/set (clojure.core/keys this__5606__auto__))) | |
(clojure.core/clear | |
[this__5605__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/putAll | |
[this__5603__auto__ m__5604__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/remove | |
[this__5601__auto__ k__5602__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/put | |
[this__5598__auto__ k__5599__auto__ v__5600__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/get | |
[this__5596__auto__ k__5597__auto__] | |
(.valAt this__5596__auto__ k__5597__auto__)) | |
(clojure.core/containsValue | |
[this__5594__auto__ v__5595__auto__] | |
(clojure.core/boolean | |
(clojure.core/some | |
#{v__5595__auto__} | |
(clojure.core/vals this__5594__auto__)))) | |
(clojure.core/isEmpty | |
[this__5593__auto__] | |
(clojure.core/= 0 (.count this__5593__auto__))) | |
(clojure.core/size [this__5592__auto__] (.count this__5592__auto__)) | |
(clojure.core/without | |
[this__5590__auto__ k__5591__auto__] | |
(if | |
(clojure.core/contains? | |
#{:name :phone :email :address} | |
k__5591__auto__) | |
(clojure.core/dissoc | |
(clojure.core/with-meta | |
(clojure.core/into {} this__5590__auto__) | |
__meta) | |
k__5591__auto__) | |
(new | |
Person | |
name | |
address | |
phone | |
__meta | |
(clojure.core/not-empty | |
(clojure.core/dissoc __extmap k__5591__auto__))))) | |
(clojure.core/assoc | |
[this__5588__auto__ k__5589__auto__ G__948] | |
(clojure.core/condp | |
clojure.core/identical? | |
k__5589__auto__ | |
:name | |
(new Person G__948 email address phone __meta __extmap) | |
(new Person name G__948 address phone __meta __extmap) | |
:address | |
(new Person name email G__948 phone __meta __extmap) | |
:phone | |
(new Person name email address G__948 __meta __extmap) | |
(new | |
Person | |
name | |
address | |
phone | |
__meta | |
(clojure.core/assoc __extmap k__5589__auto__ G__948)))) | |
(clojure.core/iterator | |
[this__5587__auto__] | |
(clojure.lang.SeqIterator. (.seq this__5587__auto__))) | |
(clojure.core/seq | |
[this__5586__auto__] | |
(clojure.core/seq | |
(clojure.core/concat | |
[(new clojure.lang.MapEntry :name name) | |
(new clojure.lang.MapEntry :email email) | |
(new clojure.lang.MapEntry :address address) | |
(new clojure.lang.MapEntry :phone phone)] | |
__extmap))) | |
(clojure.core/entryAt | |
[this__5582__auto__ k__5583__auto__] | |
(clojure.core/let | |
[v__5584__auto__ | |
(.valAt this__5582__auto__ k__5583__auto__ this__5582__auto__)] | |
(clojure.core/when-not | |
(clojure.core/identical? this__5582__auto__ v__5584__auto__) | |
(clojure.lang.MapEntry. k__5583__auto__ v__5584__auto__)))) | |
(clojure.core/containsKey | |
[this__5580__auto__ k__5581__auto__] | |
(clojure.core/not | |
(clojure.core/identical? | |
this__5580__auto__ | |
(.valAt this__5580__auto__ k__5581__auto__ this__5580__auto__)))) | |
(clojure.core/equiv | |
[this__5579__auto__ G__948] | |
(clojure.core/boolean | |
(clojure.core/or | |
(clojure.core/identical? this__5579__auto__ G__948) | |
(clojure.core/when | |
(clojure.core/identical? | |
(clojure.core/class this__5579__auto__) | |
(clojure.core/class G__948)) | |
(clojure.core/let | |
[G__948 G__948] | |
(clojure.core/and | |
(clojure.core/= name (. G__948 -name)) | |
(clojure.core/= email (. G__948 -email)) | |
(clojure.core/= address (. G__948 -address)) | |
(clojure.core/= phone (. G__948 -phone)) | |
(clojure.core/= __extmap (. G__948 __extmap)))))))) | |
(clojure.core/cons | |
[this__5577__auto__ e__5578__auto__] | |
(#'clojure.core/imap-cons this__5577__auto__ e__5578__auto__)) | |
(clojure.core/empty | |
[this__5576__auto__] | |
(throw | |
(java.lang.UnsupportedOperationException. | |
(clojure.core/str "Can't create empty: " "user.Person")))) | |
(clojure.core/count | |
[this__5575__auto__] | |
(clojure.core/+ 4 (clojure.core/count __extmap))) | |
(clojure.core/getLookupThunk | |
[this__5573__auto__ k__5574__auto__] | |
(clojure.core/let | |
[gclass (clojure.core/class this__5573__auto__)] | |
(clojure.core/case | |
k__5574__auto__ | |
:name | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -name) | |
thunk))) | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -email) | |
thunk))) | |
:address | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -address) | |
thunk))) | |
:phone | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -phone) | |
thunk))) | |
nil))) | |
(clojure.core/valAt | |
[this__5570__auto__ k__5571__auto__ else__5572__auto__] | |
(clojure.core/case | |
k__5571__auto__ | |
:name | |
name | |
:address | |
address | |
:phone | |
phone | |
(clojure.core/get __extmap k__5571__auto__ else__5572__auto__))) | |
(clojure.core/valAt | |
[this__5568__auto__ k__5569__auto__] | |
(.valAt this__5568__auto__ k__5569__auto__ nil)) | |
(clojure.core/withMeta | |
[this__5567__auto__ G__948] | |
(new Person name email address phone G__948 __extmap)) | |
(clojure.core/meta [this__5566__auto__] __meta) | |
(clojure.core/equals | |
[this__5565__auto__ G__948] | |
(clojure.lang.APersistentMap/mapEquals this__5565__auto__ G__948)) | |
(clojure.core/hashCode | |
[this__5564__auto__] | |
(clojure.lang.APersistentMap/mapHash this__5564__auto__))) | |
(clojure.core/import user.Person) | |
(clojure.core/defn | |
->Person | |
"Positional factory function for class user.Person." | |
[name email address phone] | |
(new user.Person name email address phone)) | |
(clojure.core/defn | |
map->Person | |
"Factory function for class user.Person, taking a map of keywords to field values." | |
([m__5665__auto__] (user.Person/create m__5665__auto__))) | |
user.Person) | |
nil | |
user=> (doc defrecord) | |
------------------------- | |
clojure.core/defrecord | |
([name [& fields] & opts+specs]) | |
Macro | |
Alpha - subject to change | |
(defrecord name [fields*] options* specs*) | |
Currently there are no options. | |
Each spec consists of a protocol or interface name followed by zero | |
or more method bodies: | |
protocol-or-interface-or-Object | |
(methodName [args*] body)* | |
Dynamically generates compiled bytecode for class with the given | |
name, in a package with the same name as the current namespace, the | |
given fields, and, optionally, methods for protocols and/or | |
interfaces. | |
The class will have the (immutable) fields named by | |
fields, which can have type hints. Protocols/interfaces and methods | |
are optional. The only methods that can be supplied are those | |
declared in the protocols/interfaces. Note that method bodies are | |
not closures, the local environment includes only the named fields, | |
and those fields can be accessed directy. | |
Method definitions take the form: | |
(methodname [args*] body) | |
The argument and return types can be hinted on the arg and | |
methodname symbols. If not supplied, they will be inferred, so type | |
hints should be reserved for disambiguation. | |
Methods should be supplied for all methods of the desired | |
protocol(s) and interface(s). You can also define overrides for | |
methods of Object. Note that a parameter must be supplied to | |
correspond to the target object ('this' in Java parlance). Thus | |
methods for interfaces will take one more argument than do the | |
interface declarations. Note also that recur calls to the method | |
head should *not* pass the target object, it will be supplied | |
automatically and can not be substituted. | |
In the method bodies, the (unqualified) name can be used to name the | |
class (for calls to new, instance? etc). | |
The class will have implementations of several (clojure.lang) | |
interfaces generated automatically: IObj (metadata support) and | |
IPersistentMap, and all of their superinterfaces. | |
In addition, defrecord will define type-and-value-based =, | |
and will defined Java .hashCode and .equals consistent with the | |
contract for java.util.Map. | |
When AOT compiling, generates compiled bytecode for a class with the | |
given name (a symbol), prepends the current ns as the package, and | |
writes the .class file to the *compile-path* directory. | |
Two constructors will be defined, one taking the designated fields | |
followed by a metadata map (nil for none) and an extension field | |
map (nil for none), and one taking only the fields (using nil for | |
meta and extension fields). Note that the field names __meta | |
and __extmap are currently reserved and should not be used when | |
defining your own records. | |
Given (defrecord TypeName ...), two factory functions will be | |
defined: ->TypeName, taking positional parameters for the fields, | |
and map->TypeName, taking a map of keywords to field values. | |
nil | |
user=> (doc deftype) | |
------------------------- | |
clojure.core/deftype | |
([name [& fields] & opts+specs]) | |
Macro | |
Alpha - subject to change | |
(deftype name [fields*] options* specs*) | |
Currently there are no options. | |
Each spec consists of a protocol or interface name followed by zero | |
or more method bodies: | |
protocol-or-interface-or-Object | |
(methodName [args*] body)* | |
Dynamically generates compiled bytecode for class with the given | |
name, in a package with the same name as the current namespace, the | |
given fields, and, optionally, methods for protocols and/or | |
interfaces. | |
The class will have the (by default, immutable) fields named by | |
fields, which can have type hints. Protocols/interfaces and methods | |
are optional. The only methods that can be supplied are those | |
declared in the protocols/interfaces. Note that method bodies are | |
not closures, the local environment includes only the named fields, | |
and those fields can be accessed directy. Fields can be qualified | |
with the metadata :volatile-mutable true or :unsynchronized-mutable | |
true, at which point (set! afield aval) will be supported in method | |
bodies. Note well that mutable fields are extremely difficult to use | |
correctly, and are present only to facilitate the building of higher | |
level constructs, such as Clojure's reference types, in Clojure | |
itself. They are for experts only - if the semantics and | |
implications of :volatile-mutable or :unsynchronized-mutable are not | |
immediately apparent to you, you should not be using them. | |
Method definitions take the form: | |
(methodname [args*] body) | |
The argument and return types can be hinted on the arg and | |
methodname symbols. If not supplied, they will be inferred, so type | |
hints should be reserved for disambiguation. | |
Methods should be supplied for all methods of the desired | |
protocol(s) and interface(s). You can also define overrides for | |
methods of Object. Note that a parameter must be supplied to | |
correspond to the target object ('this' in Java parlance). Thus | |
methods for interfaces will take one more argument than do the | |
interface declarations. Note also that recur calls to the method | |
head should *not* pass the target object, it will be supplied | |
automatically and can not be substituted. | |
In the method bodies, the (unqualified) name can be used to name the | |
class (for calls to new, instance? etc). | |
When AOT compiling, generates compiled bytecode for a class with the | |
given name (a symbol), prepends the current ns as the package, and | |
writes the .class file to the *compile-path* directory. | |
One constructor will be defined, taking the designated fields. Note | |
that the field names __meta and __extmap are currently reserved and | |
should not be used when defining your own types. | |
Given (deftype TypeName ...), a factory function called ->TypeName | |
will be defined, taking positional parameters for the fields | |
nil | |
user=> (comment "Ok...let's expand it all out.") | |
nil | |
user=> (pprint (macroexpand '(defrecord Person [name email address phone]))) | |
(let* | |
[] | |
(clojure.core/declare ->Person) | |
(clojure.core/declare map->Person) | |
(deftype* | |
Person | |
user.Person | |
[name email address phone __meta __extmap] | |
:implements | |
[clojure.lang.IRecord | |
clojure.lang.IObj | |
clojure.lang.ILookup | |
clojure.lang.IKeywordLookup | |
clojure.lang.IPersistentMap | |
java.util.Map | |
java.io.Serializable] | |
(clojure.core/entrySet | |
[this__5608__auto__] | |
(clojure.core/set this__5608__auto__)) | |
(clojure.core/values | |
[this__5607__auto__] | |
(clojure.core/vals this__5607__auto__)) | |
(clojure.core/keySet | |
[this__5606__auto__] | |
(clojure.core/set (clojure.core/keys this__5606__auto__))) | |
(clojure.core/clear | |
[this__5605__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/putAll | |
[this__5603__auto__ m__5604__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/remove | |
[this__5601__auto__ k__5602__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/put | |
[this__5598__auto__ k__5599__auto__ v__5600__auto__] | |
(throw (java.lang.UnsupportedOperationException.))) | |
(clojure.core/get | |
[this__5596__auto__ k__5597__auto__] | |
(.valAt this__5596__auto__ k__5597__auto__)) | |
(clojure.core/containsValue | |
[this__5594__auto__ v__5595__auto__] | |
(clojure.core/boolean | |
(clojure.core/some | |
#{v__5595__auto__} | |
(clojure.core/vals this__5594__auto__)))) | |
(clojure.core/isEmpty | |
[this__5593__auto__] | |
(clojure.core/= 0 (.count this__5593__auto__))) | |
(clojure.core/size [this__5592__auto__] (.count this__5592__auto__)) | |
(clojure.core/without | |
[this__5590__auto__ k__5591__auto__] | |
(if | |
(clojure.core/contains? | |
#{:name :phone :email :address} | |
k__5591__auto__) | |
(clojure.core/dissoc | |
(clojure.core/with-meta | |
(clojure.core/into {} this__5590__auto__) | |
__meta) | |
k__5591__auto__) | |
(new | |
Person | |
name | |
address | |
phone | |
__meta | |
(clojure.core/not-empty | |
(clojure.core/dissoc __extmap k__5591__auto__))))) | |
(clojure.core/assoc | |
[this__5588__auto__ k__5589__auto__ G__955] | |
(clojure.core/condp | |
clojure.core/identical? | |
k__5589__auto__ | |
:name | |
(new Person G__955 email address phone __meta __extmap) | |
(new Person name G__955 address phone __meta __extmap) | |
:address | |
(new Person name email G__955 phone __meta __extmap) | |
:phone | |
(new Person name email address G__955 __meta __extmap) | |
(new | |
Person | |
name | |
address | |
phone | |
__meta | |
(clojure.core/assoc __extmap k__5589__auto__ G__955)))) | |
(clojure.core/iterator | |
[this__5587__auto__] | |
(clojure.lang.SeqIterator. (.seq this__5587__auto__))) | |
(clojure.core/seq | |
[this__5586__auto__] | |
(clojure.core/seq | |
(clojure.core/concat | |
[(new clojure.lang.MapEntry :name name) | |
(new clojure.lang.MapEntry :email email) | |
(new clojure.lang.MapEntry :address address) | |
(new clojure.lang.MapEntry :phone phone)] | |
__extmap))) | |
(clojure.core/entryAt | |
[this__5582__auto__ k__5583__auto__] | |
(clojure.core/let | |
[v__5584__auto__ | |
(.valAt this__5582__auto__ k__5583__auto__ this__5582__auto__)] | |
(clojure.core/when-not | |
(clojure.core/identical? this__5582__auto__ v__5584__auto__) | |
(clojure.lang.MapEntry. k__5583__auto__ v__5584__auto__)))) | |
(clojure.core/containsKey | |
[this__5580__auto__ k__5581__auto__] | |
(clojure.core/not | |
(clojure.core/identical? | |
this__5580__auto__ | |
(.valAt this__5580__auto__ k__5581__auto__ this__5580__auto__)))) | |
(clojure.core/equiv | |
[this__5579__auto__ G__955] | |
(clojure.core/boolean | |
(clojure.core/or | |
(clojure.core/identical? this__5579__auto__ G__955) | |
(clojure.core/when | |
(clojure.core/identical? | |
(clojure.core/class this__5579__auto__) | |
(clojure.core/class G__955)) | |
(clojure.core/let | |
[G__955 G__955] | |
(clojure.core/and | |
(clojure.core/= name (. G__955 -name)) | |
(clojure.core/= email (. G__955 -email)) | |
(clojure.core/= address (. G__955 -address)) | |
(clojure.core/= phone (. G__955 -phone)) | |
(clojure.core/= __extmap (. G__955 __extmap)))))))) | |
(clojure.core/cons | |
[this__5577__auto__ e__5578__auto__] | |
(#'clojure.core/imap-cons this__5577__auto__ e__5578__auto__)) | |
(clojure.core/empty | |
[this__5576__auto__] | |
(throw | |
(java.lang.UnsupportedOperationException. | |
(clojure.core/str "Can't create empty: " "user.Person")))) | |
(clojure.core/count | |
[this__5575__auto__] | |
(clojure.core/+ 4 (clojure.core/count __extmap))) | |
(clojure.core/getLookupThunk | |
[this__5573__auto__ k__5574__auto__] | |
(clojure.core/let | |
[gclass (clojure.core/class this__5573__auto__)] | |
(clojure.core/case | |
k__5574__auto__ | |
:name | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -name) | |
thunk))) | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -email) | |
thunk))) | |
:address | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -address) | |
thunk))) | |
:phone | |
(clojure.core/reify | |
clojure.lang.ILookupThunk | |
(clojure.core/get | |
[thunk gtarget] | |
(if | |
(clojure.core/identical? (clojure.core/class gtarget) gclass) | |
(. gtarget -phone) | |
thunk))) | |
nil))) | |
(clojure.core/valAt | |
[this__5570__auto__ k__5571__auto__ else__5572__auto__] | |
(clojure.core/case | |
k__5571__auto__ | |
:name | |
name | |
:address | |
address | |
:phone | |
phone | |
(clojure.core/get __extmap k__5571__auto__ else__5572__auto__))) | |
(clojure.core/valAt | |
[this__5568__auto__ k__5569__auto__] | |
(.valAt this__5568__auto__ k__5569__auto__ nil)) | |
(clojure.core/withMeta | |
[this__5567__auto__ G__955] | |
(new Person name email address phone G__955 __extmap)) | |
(clojure.core/meta [this__5566__auto__] __meta) | |
(clojure.core/equals | |
[this__5565__auto__ G__955] | |
(clojure.lang.APersistentMap/mapEquals this__5565__auto__ G__955)) | |
(clojure.core/hashCode | |
[this__5564__auto__] | |
(clojure.lang.APersistentMap/mapHash this__5564__auto__))) | |
(clojure.core/import user.Person) | |
(clojure.core/defn | |
->Person | |
"Positional factory function for class user.Person." | |
[name email address phone] | |
(new user.Person name email address phone)) | |
(clojure.core/defn | |
map->Person | |
"Factory function for class user.Person, taking a map of keywords to field values." | |
([m__5665__auto__] (user.Person/create m__5665__auto__))) | |
user.Person) | |
nil | |
user=> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment