23
This commit is contained in:
parent
5674c49c3a
commit
91bccfa55f
1 changed files with 10 additions and 10 deletions
|
|
@ -7,13 +7,13 @@
|
||||||
|
|
||||||
(meditations
|
(meditations
|
||||||
"Some objects can be tagged using the with-meta function"
|
"Some objects can be tagged using the with-meta function"
|
||||||
(= __ (meta giants))
|
(= {:league "National League"} (meta giants))
|
||||||
|
|
||||||
"Or more succinctly with a reader macro"
|
"Or more succinctly with a reader macro"
|
||||||
(= __ (meta '^{:division "West"} Giants))
|
(= {:division "West"} (meta '^{:division "West"} Giants))
|
||||||
|
|
||||||
"While others can't"
|
"While others can't"
|
||||||
(= __ (try
|
(= "This doesn't implement the IObj interface" (try
|
||||||
(with-meta
|
(with-meta
|
||||||
2
|
2
|
||||||
{:prime true})
|
{:prime true})
|
||||||
|
|
@ -21,31 +21,31 @@
|
||||||
"This doesn't implement the IObj interface")))
|
"This doesn't implement the IObj interface")))
|
||||||
|
|
||||||
"Notice when metadata carries over"
|
"Notice when metadata carries over"
|
||||||
(= __ (meta (merge '^{:foo :bar} {:a 1 :b 2}
|
(= {:foo :bar} (meta (merge '^{:foo :bar} {:a 1 :b 2}
|
||||||
{:b 3 :c 4})))
|
{:b 3 :c 4})))
|
||||||
|
|
||||||
"And when it doesn't"
|
"And when it doesn't"
|
||||||
(= __ (meta (merge {:a 1 :b 2}
|
(= nil (meta (merge {:a 1 :b 2}
|
||||||
'^{:foo :bar} {:b 3 :c 4})))
|
'^{:foo :bar} {:b 3 :c 4})))
|
||||||
|
|
||||||
"Metadata can be used as a type hint to avoid reflection during runtime"
|
"Metadata can be used as a type hint to avoid reflection during runtime"
|
||||||
(= __ (#(.charAt ^String % 0) "Cast me"))
|
(= \C (#(.charAt ^String % 0) "Cast me"))
|
||||||
|
|
||||||
"You can directly update an object's metadata"
|
"You can directly update an object's metadata"
|
||||||
(= 8 (let [giants
|
(= 8 (let [giants
|
||||||
(with-meta
|
(with-meta
|
||||||
'Giants
|
'Giants
|
||||||
{:world-series-titles (atom 7)})]
|
{:world-series-titles (atom 7)})]
|
||||||
(swap! (:world-series-titles (meta giants)) __)
|
(swap! (:world-series-titles (meta giants)) inc)
|
||||||
@(:world-series-titles (meta giants))))
|
@(:world-series-titles (meta giants))))
|
||||||
|
|
||||||
"You can also create a new object from another object with metadata"
|
"You can also create a new object from another object with metadata"
|
||||||
(= {:league "National League" :park "Oracle Park"}
|
(= {:league "National League" :park "Oracle Park"}
|
||||||
(meta (vary-meta giants
|
(meta (vary-meta giants
|
||||||
assoc __ __)))
|
assoc :league "National League" :park "Oracle Park")))
|
||||||
|
|
||||||
"But it won't affect behavior like equality"
|
"But it won't affect behavior like equality"
|
||||||
(= __ (vary-meta giants dissoc :league))
|
(= giants (vary-meta giants dissoc :league))
|
||||||
|
|
||||||
"Or the object's printed representation"
|
"Or the object's printed representation"
|
||||||
(= __ (pr-str (vary-meta giants dissoc :league))))
|
(= "Giants" (pr-str (vary-meta giants dissoc :league))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue