Why can’t the operator ‘==’ be applied to a struct and default(struct)?

For classes, the == operator uses reference equality. Of course, structs are value types, so they can’t be compared by reference. There is no default implementation of == for structs because memberwise comparison isn’t always a valid comparison, depending on the type. You can instead use the Object.Equals method, which does compare memberwise: Console.WriteLine(user.Equals(default(User)) ? … Read more

Why do I get an error when adding an integer to a floating point?

The technically correct answer is: because no one has written impl Add<f64> for i32 {}. The cheeky answer is: because Rust doesn’t want you to shoot yourself in the foot. The longer, potentially more useful answer is… In computers, integers and floating point numbers both have a limited range, ultimately driven by the number of … Read more

Equality in Kotlin

Referential Equality Java In Java, the default implementation of equals compares the variable’s reference, which is what == always does: The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and … Read more