Skip to content

fixed types in equality operator#3047

Open
ardakalayci wants to merge 1 commit intojonataslaw:masterfrom
ardakalayci:equality-operator
Open

fixed types in equality operator#3047
ardakalayci wants to merge 1 commit intojonataslaw:masterfrom
ardakalayci:equality-operator

Conversation

@ardakalayci
Copy link
Copy Markdown

When hashcode is not used, we can only be sure about whether primitive types have changed or not. However, for lists and maps, we need to check with hashcode.

@jonataslaw
Copy link
Copy Markdown
Owner

Can you explain this to me better?
The equality operator by default compares the hashCode of two objects.
A list will only be equal to another list if both are const. This doesn't seem to have anything to do with the hashcode.
Only immutable lists are the same as immutable lists.

void main() {
  
  List foo = [1,2,4, "eee"];
  List bar = [1,2,4, "eee"];
  
  print(foo == bar); // false
  print(foo.hashCode == bar.hashCode); // false
}
void main() {
  
  List foo = const  [1,2,4, "eee"];
  List bar = const [1,2,4, "eee"];
  
  print(foo == bar); // true
  print(foo.hashCode == bar.hashCode); // true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants