Leif <leif.poor...@gmail.com> writes:

> I'd also like to make sure people are aware of this oddity. I
> discovered this after reading an article about the bad design of PHP.
> I read that in PHP, "== is not transitive." I thought "Ha ha ha, that
> ridiculous PHP!"
>
> Then I checked c.c/== ; Imagine my reaction when I learned that
> Clojure had something in common with PHP. o_O, :'[ Other emoticons
> also washed over me.

Clojure's == is numerical equivalence, not equality.

    -------------------------
    clojure.core/==
    ([x] [x y] [x y & more])
      Returns non-nil if nums all have the equivalent
      value (type-independent), otherwise false

The problem with PHP (and JavaScript's) == operator is that the type
coercion is not limited to numerical types:

PHP

    "1" == 1    => true
    "1" + 1     => 2

JavaScript

    "1" == 1    => true
    "1" + 1     => "11"

Clojure

    (== "1" 1)  => java.lang.ClassCastException
    (+ "1" 1)   => java.lang.ClassCastException

Clojure's equality function [1], however, does what you expect when
comparing floats and integers:

    (= 1.0 1)   => false

But it also does what you want when comparing _categories_ of numbers
[2] [3]:

    (let [a (int 1) b (bigint 1)]
      {:equal_values? (= a b)
       :equal_types?  (= (type a) (type b))})

    => {:equal_values? true, :equal_types? false}

I think Clojure does a fantastic job of being both convenient and
strongly typed.

--
guns

[1]: 
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Util.java#L23
[2]: 
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Numbers.java#L213
[3]: 
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Numbers.java#L978

     If you were really concerned about whether a number is a BigInteger
     vs Integer, or Float vs Double, you would have to handle that on
     your own.

-- 
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