equality

Equality relations in Scala

这一生的挚爱 提交于 2019-11-30 19:26:46
I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world system, with persistence or serialization, I cannot rely on identity to tell me whether two references

Cast to object before null check in overriding Equals [duplicate]

北城余情 提交于 2019-11-30 18:44:16
This question already has an answer here: In the msdn guidance on Equals override, why the cast to object in the null check? 3 answers Just reading the msdn article on overriding equality operators here The following snippet confuses me... // If parameter cannot be cast to Point return false. TwoDPoint p = obj as TwoDPoint; if ((System.Object)p == null) // <-- wtf? { return false; } Why is there a cast to Object here to perform the null comparison? Operators apply through static analysis (and overloads), not virtual methods (overrides). With the cast, it is doing a reference equality check.

Why is (18446744073709551615 == -1) true?

假装没事ソ 提交于 2019-11-30 17:48:25
When I was working on string::npos I noticed something and I couldn't find any explanation for it on the web. (string::npos == ULONG_MAX) and (string::npos == -1) are true. So I tried this: (18446744073709551615 == -1) which is also true. How can it be possible? Is it because of binary conversation? Evan Carroll 18,446,744,073,709,551,615 This number mentioned, 18,446,744,073,709,551,615 , is actually 2^64 − 1 . The important thing here is that 2^64-1 is essentially 0-based 2^64 . The first digit of an unsigned integer is 0 , not 1 . So if the maximum value is 1 , it has two possible values: 0

Equality between two enumerables

一世执手 提交于 2019-11-30 17:05:28
I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true. As a side question, the code below to compare each element works, but there must be a more elegant way var other = (ActivityService) obj; if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false; for (int i = 0; i < AllAccounts.Count(); i++) { if (!AllAccounts.ElementAt(i).Equals(other.AllAccounts.ElementAt(i))) { return false; } } return true; Have a look at the Enumerable.SequenceEqual method . bool result = AllAccounts.SequenceEqual(other.AllAccounts); Depending on the

== for pointer comparison

本秂侑毒 提交于 2019-11-30 16:54:39
I quote from "The C Programming Language" by Kernighan & Ritchie: Any pointer can be meaningfully compared for equality or inequality with zero. But the behavior is undefined for arithmetic or comparisons with pointers that do not point to members of the same array. (There is one exception: the address of the first element past the end of an array can be used in pointer arithmetic.) Does this mean I cannot rely on == for checking equality of different pointers? What are the situations in which this comparison leads to a wrong result? One example that comes to my mind is Harvard architecture

Is there an idiomatic approach in C++ for comparing polymorphic types for object equivalence?

风格不统一 提交于 2019-11-30 15:12:27
问题 I have Base* pointers to two instances of a polymorphic type and I need to determine if the referenced objects are equivalent. My current approach is to first use RTTI to check for type equality. If the types are equal, I then call a virtual is_equivalent function. Is there a more idiomatic approach? 回答1: For most of the derived classes, equivalent simply means that the member variables all the same value In C++ this is called 'equality' and is usually implemented using operator==() . In C++

When are two enums equal in C#?

断了今生、忘了曾经 提交于 2019-11-30 14:11:03
问题 I have created two enums and I know they are not the same but still I think it makes sense they would be equal since their string representation as well as their numeral representation are equal (and even the same...). In other words : I would like the first test to pass and the second one to fail. In reality however, they both fail. So : when are two enums in C# equal? Or is there anyway to define the equals operator in C#? Thanks! public enum enumA {one, two} public enum enumB {one, two}

Is there an idiomatic approach in C++ for comparing polymorphic types for object equivalence?

◇◆丶佛笑我妖孽 提交于 2019-11-30 14:04:32
I have Base* pointers to two instances of a polymorphic type and I need to determine if the referenced objects are equivalent. My current approach is to first use RTTI to check for type equality. If the types are equal, I then call a virtual is_equivalent function. Is there a more idiomatic approach? For most of the derived classes, equivalent simply means that the member variables all the same value In C++ this is called 'equality' and is usually implemented using operator==() . In C++ you can override the meaning of operators, it is possible to write: MyType A; MyType B; if (A == B) { // do

Does a Python object which doesn't override comparison operators equals itself?

荒凉一梦 提交于 2019-11-30 11:45:19
class A(object): def __init__(self, value): self.value = value x = A(1) y = A(2) q = [x, y] q.remove(y) I want to remove from the list a specific object which was added before to it and to which I still have a reference. I do not want an equality test. I want an identity test. This code seems to work in both CPython and IronPython, but does the language guarantee this behavior or is it just a fluke? The list.remove method documentation is this: same as del s[s.index(x)] , which implies that an equality test is performed. So will an object be equal to itself if you don't override __cmp__ , __eq

Strings don't seem to be equal in Java on Android, even though they print the same

孤街浪徒 提交于 2019-11-30 11:34:19
I've got a problem that I'm rather confused about. I have the following lines of code in my android application: System.out.println(CurrentNode.getNodeName().toString()); if (CurrentNode.getNodeName().toString() == "start") { System.out.println("Yes it does!"); } else { System.out.println("No it doesnt"); } When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else statement and prints "No it doesn't". I wondered if the name of the node might have some kind of non-printing