You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My understanding is that == is used to check if two objects pointers refer to the same location (both objects are same) and it is already defined in ProtoObject, there is no reason to override it.
The way PMVector == is implemented, it compares the contents of the object and not the memory location. Renaming it to something like isEqualTo (or =?) would be better.
@AtharvaKhare Your analysis is correct, but I'd go farther in the conclusion: overriding == is not only useless but potentially dangerous because it breaks the semantics of the operation.
You suggest renaming it, but do you actually see any situation in which it might be useful? Note that = is already there (inherited from SequenceableCollection), but does something different from PMVector>>==.
Also, looking at the comment and the syntax == aNumber, I think it is intended to apply equality to every element of the vector with a number, so it is not the same as =, which tests for equal contents between objects.
So I think it's use is like this (when rename to something like isEqualTo:
x := #(1 1 1) asPMVector.
x isEqualToThis: 1
>>> a PMVector(true true true)
I also have a question about the API, why do these arithmetic operators (>, <, isEqualTo) modify the object and not return a new one, unlike +, -, * which return new one?
My question was rather if you see a situation where one might actually want to use it. Why would I ever want to know if all elements of a vector are one-by-one identical? I don't see a good use case, but obviously the author of that code did, so maybe I am just missing it.
Element-by-element comparison yielding a vector/series of booleans are definitely useful in some circumstances. What I don't see is the utility of testing for element-wise object identity. Identity tests a (==) are rarely used. In Pharo I mostly see them as a first step in equality tests, to avoid doing more expensive comparisons. That scenario never happens for elementwise comparison of vectors. So what would be a situation where elementwise identity matters?
== is used everytime you need to be sure objects are unique, for example in a Set. Look at == senders in your image. There are more than 9000 in my image, so this is used a lot of time actually :-)
Set uses = for comparing elements, it's IdentitySet that uses ==.
I was a bit surprised by the 9000 senders, so I decided to look at them myself. It's even 12000 in my image, but most of the methods found don't actually use ==. What they do contain instead is ifNil: and/or ifNotNil:. Perhaps the compiler replaces those by == nil. Anyway, I have no idea how to find the real uses of ==. A text search in all methods also finds ==>, comments, etc.
Activity
SergeStinckwich commentedon Mar 2, 2019
This is part of DHB original code that was copy-paste in PolyMath, so maybe we have to remove it.
AtharvaKhare commentedon May 8, 2019
Solving this issue only involves removing
PolyMath/src/Math-Core/PMVector.class.st
Lines 92 to 96 in b1ec1ef
running tests, and fix tests that dont?
SergeStinckwich commentedon May 8, 2019
Do you understand what is
==method ? and why classes should not reimplement it ?AtharvaKhare commentedon May 8, 2019
My understanding is that
==is used to check if two objects pointers refer to the same location (both objects are same) and it is already defined inProtoObject, there is no reason to override it.The way
PMVector ==is implemented, it compares the contents of the object and not the memory location. Renaming it to something likeisEqualTo(or=?) would be better.Am I correct?
khinsen commentedon May 8, 2019
@AtharvaKhare Your analysis is correct, but I'd go farther in the conclusion: overriding
==is not only useless but potentially dangerous because it breaks the semantics of the operation.You suggest renaming it, but do you actually see any situation in which it might be useful? Note that
=is already there (inherited fromSequenceableCollection), but does something different fromPMVector>>==.AtharvaKhare commentedon May 8, 2019
@khinsen right, I get it :)
Also, looking at the comment and the syntax
== aNumber, I think it is intended to applyequalityto every element of the vector with a number, so it is not the same as=, which tests for equal contents between objects.So I think it's use is like this (when rename to something like
isEqualTo:I also have a question about the API, why do these arithmetic operators (
>,<,isEqualTo) modify the object and not return a new one, unlike+,-,*which return new one?khinsen commentedon May 9, 2019
My question was rather if you see a situation where one might actually want to use it. Why would I ever want to know if all elements of a vector are one-by-one identical? I don't see a good use case, but obviously the author of that code did, so maybe I am just missing it.
AtharvaKhare commentedon May 9, 2019
I can't figure out how it would be useful for PMVector, but in Dataseries, it can be used for converting binary categories to 0-1s.
khinsen commentedon May 13, 2019
Element-by-element comparison yielding a vector/series of booleans are definitely useful in some circumstances. What I don't see is the utility of testing for element-wise object identity. Identity tests a (
==) are rarely used. In Pharo I mostly see them as a first step in equality tests, to avoid doing more expensive comparisons. That scenario never happens for elementwise comparison of vectors. So what would be a situation where elementwise identity matters?AtharvaKhare commentedon May 13, 2019
I got a bit confused between
==and=in my previous reply. I can't think of any application that would need==.SergeStinckwich commentedon May 13, 2019
==is used everytime you need to be sure objects are unique, for example in a Set. Look at==senders in your image. There are more than 9000 in my image, so this is used a lot of time actually :-)khinsen commentedon May 13, 2019
Setuses=for comparing elements, it'sIdentitySetthat uses==.I was a bit surprised by the 9000 senders, so I decided to look at them myself. It's even 12000 in my image, but most of the methods found don't actually use
==. What they do contain instead isifNil:and/orifNotNil:. Perhaps the compiler replaces those by== nil. Anyway, I have no idea how to find the real uses of==. A text search in all methods also finds==>, comments, etc.