equality

Lambda expression compilation

Hi! Given the lambda expression below where Province type contains a public property "byte CountryId" and Country type which contains a public property "byte Id". Expression<Func<Province, bool>> exp = p => p.CountryId == country.Id; The Expression is later used by NHibernate Linq provider and threw an exception. When I inspected th...

nhibernate: what are the best practices for implementing equality?

I think that Entities should implement equality by primary key comparison as default, but the nhibernate documentation recommends using business identity: The most obvious way is to implement Equals()/GetHashCode() by comparing the identifier value of both objects. If the value is the same, both must be the same database row, they a...

Object equality in .NET

I feel pretty ignorant asking this, but would someone be able to explain to me why this is happening? class MyClass{ public int i {get; set; } } class Program { static void Main(string[] args) { MyClass a = new MyClass(); MyClass b = new MyClass(); b.i = 2; a = b; a.i = 1; ...

When would JavaScript == make more sense than ===?

As 359494 indicates they are basically identical except '===' also ensures type equality and hence '==' might perform type conversion. In Douglas Crockford's JavaScript: The Good Parts, it is advised to always avoid '=='. However, I'm wondering what the original thought of designing two set of equality operators was. Have you seen any s...

Java object Equality

The two cards c1 and c4 seem to be equal...but they are not why. I want them to be equal so that only one of them is allowed in the Set. :| import java.util.*; class Card2 { private int value; private String type; public Card2(int v,String t) { value=v; type=t; } public int getValue() { return value; } public String ge...

Python __init__ argument problem

I have some trouble understanding what happens with class init arguments that are lists like: class A(object): def __init__(self, argument=[]): self.argument = argument[:] or: def __init__(self,argument=None): self.arguments = arguments or [] or: def __init__(self, argument=[]): self.argument = argume...

.NET Dictionaries have same keys and values, but aren't "equal"

This test fails: using Microsoft.VisualStudio.TestTools.UnitTesting; [TestMethod()] public void dictEqualTest() { IDictionary<string, int> dict = new Dictionary<string, int>(); IDictionary<string, int> dictClone = new Dictionary<string, int>(); for (int x = 0; x < 3; x++) { d...

CLR JIT optimizations violates causality?

I was writing an instructive example for a colleague to show him why testing floats for equality is often a bad idea. The example I went with was adding .1 ten times, and comparing against 1.0 (the one I was shown in my introductory numerical class). I was surprised to find that the two results were equal (code + output). float @float =...

In Ruby, why does a equality with nil ("Date.new == nil") return nil?

When writing some rspec today, I came across some unexpected behavior with comparing Date (and Time) instances to nil. Here's a sample using raw ruby (no Rails or other libraries): user@MacBook-Work ~ $ ruby -v ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0] user@MacBook-Work ~ $ irb >> 1 == nil => false >> "string" == nil...

When comparing for equality is it okay to use `==`?

When comparing for equality is it okay to use ==? For example: int a = 3; int b = 4; If checking for equality should you use: if (a == b) { . . . } Would the situation change if floating point numbers were used? ...

Testing for Type Equality without RTTI

Say B and C are derived from A. I want to be able to test whether any two instances of classes derived from A are instances of the same class, that is, whether A* foo and A* bar both point to B instances, without using RTTI. My current solution is something like this: class A { protected: typedef uintptr_t Code; virtual Code co...

Equality relations in Scala

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

NHibernate Equality: How do I ensure only one row is persisted from many "equal" .NET objects?

How can I get the following test to pass with NHibernate? I thought it was enough to simply override Equals and GetHashCode in the entity class for this to work the way I want it to. Obviously for "Point" objects, which are quite trivial, it's silly to persist multiple rows for identical coordinates. I have two point objects that have i...

Compare equality of char[] in C

I have two variables: char charTime[] = "TIME"; char buf[] = "SOMETHINGELSE"; I want to check if these two are equal... using charTime == buf doesn't work. What should I use, and can someone explain why using == doesn't work? Would this action be different in C and C++? ...

GetHashCode on null fields?

How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ ...

Entities equals(), hashCode() and toString(). How to correctly implement them?

I'm implementing equals(), hashCode() and toString() of my entities using all the available fields in the bean. I'm getting some Lazy init Exception on the frontend when I try to compare the equality or when I print the obj state. That's because some list in the entity can be lazy initialized. I'm wondering what's the correct way to fo...

Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?

As far as I understand, Scala's == defines the natural equality of two objects. I expected that Array(0,1,2) == Array(0,1,2) compares the natural equality e. g. checks if all elements of the array return true when compared with the corresponding elements of the other array. People told me that Scala's Array is just a Java [] which only...

Different results when applying function to equal values

I'm just digging a bit into Haskell and I started by trying to compute the Phi-Coefficient of two words in a text. However, I ran into some very strange behaviour that I cannot explain. After stripping everything down, I ended up with this code to reproduce the problem: let sumTup = (sumTuples∘concat) frequencyLists let sumFixTup = (13...

C# string equality operator returns false, but I'm pretty sure it should be true... What?!

I'm trying to write a unit test for a piece of code that generates a large amount of text. I've run into an issue where the "expected" and "actual" strings appear to be equal, but Assert.AreEqual throws, and both the equality operator and Equals() return false. The result of GetHashCode() is different for both values as well. However, p...

Equality between two enumerables

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 Cheers, Berryl var other = (ActivityService) obj; if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false; for...