Replies: 11 comments
-
Maybe I should ask in this thread:
Does it mean that a record may derive from a non-record class?
|
Beta Was this translation helpful? Give feedback.
-
I don't like the idea of not taking into consideration the equality of the base type if that base type is not a record. I think it will lead to unexpected consequences: // defined in some assembly somewhere:
public abstract class Entity {
public int ID { get; }
protected Entity(int id) { ... }
// has members Equals/GetHashCode based on the value of ID
}
// defined in my assembly:
public record Person(int ID, string First, string Last) : Entity(ID);
var person1 = new Person(100, "John", "Smith");
var person2 = new Person(101, "John", "Smith");
if (person1.Equals(person2)) {
Console.WriteLine("Oops, this is broken!");
} I reiterate that I think that the C# compiler should always call |
Beta Was this translation helpful? Give feedback.
-
I don't like the idea that records can have a base type that is not a record (or a single specific BCL root type for all records that goes unspecified by default and is an error to explicitly write). |
Beta Was this translation helpful? Give feedback.
-
@agocke |
Beta Was this translation helpful? Give feedback.
-
THe lang spec defines things in terms of simple semantics. The implementation is free to optimize as long as those semantics are preserved. |
Beta Was this translation helpful? Give feedback.
-
What can be more optimized than direct comparison on the IL level? 🤔 Note how for strings the call to GenericEqualityComparer is not inlined. The implementation is getting more and more complex in the business of generatin code. It has now tiered compilation, intepreter, ready2run, corert, mono translates to LLVM bitcode etc. |
Beta Was this translation helpful? Give feedback.
-
yes. that's what i'm saying. the implementation is free to emit that. there's no need for the language to state that that's what will happen. |
Beta Was this translation helpful? Give feedback.
-
I don't see how that's relevant. As i mentioned, the implemntation is free to inline this itself. It's out of the purview of the language.
It sounds lke you're discussing the runtime. I'm talking about the compiler. |
Beta Was this translation helpful? Give feedback.
-
The language could do the implementation a favour.
It is not clear why the compiler chose to use EqualityComparer in non-generic context (for which EqualityComparer was created). |
Beta Was this translation helpful? Give feedback.
-
Can you give an example of that?
If you would ike the compiler to more efficiently implement some language operations, you can file issues over at dotnet/roslyn. |
Beta Was this translation helpful? Give feedback.
-
Cyrus is right that I don't think we need to specify this in the language, and as to why we use EqualityComparer -- honestly I don't know. 😄 I reused the implementation for anonymous types, which use EqualityComparer. I don't see any reason why we couldn't change it, as long as the appropriate semantics are preserved. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
C# Language Design Notes for June 1, 2020
Records:
1. Base call syntax
2. Synthesizing positional record members and assignments
3. Record equality through inheritance
Beta Was this translation helpful? Give feedback.
All reactions