In Midje, I have reason to create a type Metaconstant whose instances are 
"equal" to a symbol with the same name. Here's the relevant bits of the 
definition:

    (deftype Metaconstant [name storage]
      Object
      (equals [this that]
             (if (instance? (class this) that)
               (= (.name this) (.name that))
               (= (.name this) that)))
   
      clojure.lang.IPersistentCollection
      (equiv [this that]
             (println "equiv is called with" (type this) (type that))
             (.equals this that)))

That's fine when a Metaconstant is compared to a symbol, but I thought I'd have 
to engage in hackery when a symbol is compared to a Metaconstant. That is:

    (println "= symbol metaconstant?")
    (println (= '...name... (Metaconstant. '...name... {}))) ; expect false
    (println "= metaconstant symbol?")
    (println (= (Metaconstant. '...name... {}) '...name... )) ; expect true

Surprisingly, both directions produce true. That's because the order in which 
`.equiv` is called doesn't seem to depend on the order of arguments to `=`:

    = symbol metaconstant?
    equiv is called with midje.ideas.t-m.Metaconstant clojure.lang.Symbol
    true
    = metaconstant symbol?
    equiv is called with midje.ideas.t-m.Metaconstant clojure.lang.Symbol
   true

Why is .equiv called with reordered arguments? And can I depend on that 
behavior going forward?

I see this behavior with 1.2.0, 1.2.1, and 1.3.0-beta1.


-----
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Occasional consulting on Agile
www.exampler.com, www.twitter.com/marick

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

Reply via email to