public
Last active

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.

  • Download Gist
replsession.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
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
email
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)
:email
(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
email
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)))
:email
(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
:email
email
: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
email
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)
:email
(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
email
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)))
:email
(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
:email
email
: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=>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.