When you compare functions, it only checks if it is the same function
object (not if the function "behaves" the same way).

For example:
(= (fn []) (fn []))
;=> false

The reason you get false in your case is because with-meta returns a new
object every time you call it.
We need a new object to keep the immutability.

If you want to check whether two things are the same object, you can use
identical?, as so:

(identical? (fn []) (fn []))
;=> false
(identical? {:b 3} {:b 3})
;=> false
(def t {:b 3})
(identical? t t)
;=> true
(identical? t (with-meta t {}))
;=> false

As you can see on the last line, with meta returns a new object.
Note that:

(= t (with-meta t {}))
;=> true

This is because = for maps check the actual contents of the map,
as opposed to how it works with functions
where it only checks if it is the same object.

Jonathan


On Fri, Nov 23, 2012 at 7:12 PM, N8Dawgrr <nathan.r.matth...@gmail.com>wrote:

> I have unexplained behavior for with-meta.
>
> As far as I understand with-meta should not alter object identity. E.g. if
> we have the (= a b) => true for some a and b then
> (= (with-meta a ma) (with-meta b mb)) => true should also hold for any ma
> and mb.
>
> So why do I get the following behavior at the REPL?
>
> user> (def f (partial * 2))
>
> user> (= f f)
> true
>
> user> (= (with-meta f {:a 1}) (with-meta f {:a 1}))
> false
>
> Any help appreciated.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to