Overriding the == operator, like in Java/Lua/Python/whatever.

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding the == operator, like in Java/Lua/Python/whatever.

Soni L.
I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Overriding the == operator, like in Java/Lua/Python/whatever.

Caitlin Potter
Unfortunately, there's no "real" way to do this as of yet, short of abusing ToPrimitive in certain cases, far from ideal.

I'm sure someone must have proposed it at some point, but perhaps not. In the mean time, it might be a nice feature of a compile-to-js language like coffeescript or typescript to experiment with

On Jan 3, 2015, at 8:52 PM, Soni L. <[hidden email]> wrote:

I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Overriding the == operator, like in Java/Lua/Python/whatever.

Salvador de la Puente González-2
In reply to this post by Soni L.

Reimplement Object#valueOf() returning a string representation of your object.

See http://webreflection.blogspot.com.es/2010/10/javascript-coercion-demystified.html?m=1

Hope it helps.

On 4 Jan 2015 02:53, "Soni L." <[hidden email]> wrote:
I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Overriding the == operator, like in Java/Lua/Python/whatever.

Michał Wadas

It won't help because objects are ALWAYS compared by their identity. valeOf or toString will be used only in case of comparing object with primitive.

4 sty 2015 03:10 "Salvador de la Puente González" <[hidden email]> napisał(a):

Reimplement Object#valueOf() returning a string representation of your object.

See http://webreflection.blogspot.com.es/2010/10/javascript-coercion-demystified.html?m=1

Hope it helps.

On 4 Jan 2015 02:53, "Soni L." <[hidden email]> wrote:
I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Overriding the == operator, like in Java/Lua/Python/whatever.

Sebastian McKenzie
In reply to this post by Caitlin Potter
http://www.slideshare.net/BrendanEich/value-objects2




On Sun, Jan 4, 2015 at 1:08 PM, Caitlin Potter <[hidden email]> wrote:

Unfortunately, there's no "real" way to do this as of yet, short of abusing ToPrimitive in certain cases, far from ideal.

I'm sure someone must have proposed it at some point, but perhaps not. In the mean time, it might be a nice feature of a compile-to-js language like coffeescript or typescript to experiment with

On Jan 3, 2015, at 8:52 PM, Soni L. <[hidden email]> wrote:

I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Overriding the == operator, like in Java/Lua/Python/whatever.

Andrea Giammarchi-2
In reply to this post by Michał Wadas
yep, I'm afraid this is described as such in the old post (and in specs) too:

Return true if x and y refer to the same object. Otherwise, return false

in your Point cases you can add `.equals(otherPoint)` in Point prototype otherwise use an utility that does similar thing Java does.

Regards



On Sun, Jan 4, 2015 at 3:16 AM, Michał Wadas <[hidden email]> wrote:

It won't help because objects are ALWAYS compared by their identity. valeOf or toString will be used only in case of comparing object with primitive.

4 sty 2015 03:10 "Salvador de la Puente González" <[hidden email]> napisał(a):

Reimplement Object#valueOf() returning a string representation of your object.

See http://webreflection.blogspot.com.es/2010/10/javascript-coercion-demystified.html?m=1

Hope it helps.

On 4 Jan 2015 02:53, "Soni L." <[hidden email]> wrote:
I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Overriding the == operator, like in Java/Lua/Python/whatever.

Gary Guo
In reply to this post by Soni L.
I agree that use equals is a better choice. If operator overloading is implemented, you may experience inconsistency (different instance of builtin boxed values would be unequal according to default semantics, while after overloaded == operator it becomes equal.)


Date: Sat, 3 Jan 2015 23:52:58 -0200
From: [hidden email]
To: [hidden email]
Subject: Overriding the == operator, like in Java/Lua/Python/whatever.

I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Overriding the == operator, like in Java/Lua/Python/whatever.

Gary Guo
In reply to this post by Soni L.
I agree that use equals is a better choice. If operator overloading is implemented, you may experience inconsistency (different instance of builtin boxed values would be unequal according to default semantics, while after overloaded == operator it becomes equal.)


Date: Sat, 3 Jan 2015 23:52:58 -0200
From: [hidden email]
To: [hidden email]
Subject: Overriding the == operator, like in Java/Lua/Python/whatever.

I've been looking and looking and looking and I couldn't find a way to override == to make 2 different instances of MyPoint compare == if they have the same coordinates. (I guess this also applies to String objects and stuff but I haven't tested that)

So how do I do this?

(PS: yes, I know, Java has an .equals method, not overridable ==, but it's basically the same thing)

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss