Operator overloading for non-value objects

Claude Pache claude.pache at gmail.com
Tue Jan 14 06:00:39 PST 2014

Le 13 janv. 2014 à 11:45, Anne van Kesteren <annevk at annevk.nl> a écrit :

> In a discussion I had with Alex Russell as how to do comparison for
> URL objects it ended up with desiring
>  url == url2
> to work. It escaped me at that point that I already discussed this
> briefly and Brendan explained why face-to-face. However, I forgot what
> he said :/
> The alternative, either something like
>  url.equals(url2)
> or
>  URL.equal(url, url2)
> or
>  url.toString() == url2.toString()
> is somewhat Java-esque. Is that what we should do? And if so, opinions
> on which variant?
> -- 
> http://annevankesteren.nl/
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

Consider the following objects:

    url, url2, ... : URL objects
    location, location2, ... : Location objects
    a, a2, .... : HTMLAnchorElement objects

All these objects implement URLUtils according to the WhatWG specs, and are therefore stringified using their `href` attribute. 
Now, which one of these equalities should "work" by just comparing the stringification?

    url == url2
    location == url
    location == location2
    a == url
    a == location
    a == a2

For me, I couldn't say. But in any case, my intention is clearer (and not too Java-esque) by writing the following:

    a.href == location.href

In the worst case, when I don't know if I have a string or an URLUtils object, I just ensure that at least one member of the equality operator is stringified—and, most importantly, that it is evident from reading my code that one member is stringified:

    a.href == url
    String(whatever) == url


More information about the es-discuss mailing list