You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UPDATE: As mentioned by @eernstg in the comment below, the non-trivial equality of objects should affect the constant sets with such objects, not the ability of the objects to be constant. The original contents of the issue is left at the bottom, for archival purposes.
The following program is compiled by the CFE without any compile-time error.
A compile-time error is expected because A declares a non-trivial equality, and records containing such objects can't be elements of constant sets.
The analyzer displays the following error message for that program.
error - asdf.dart:7:25 - The type of an element in a constant set can't override the '==' operator, but the type '(A)' does. Try using a different value for the element, or removing the keyword 'const' from the set. - const_set_element_type_implements_equals
THE ORIGINAL ISSUE CONTENTS:
The following code is accepted by the CFE, but a compile-time error is expected. Note that const A() is accepted even outside of record literals.
This is not a problem. A class can have a constant constructor and be used in constant expressions, and still have an operator == that runs any code it wants. However, there should be an error if const A() is used in a situation where it is required to have a primitive operator ==, like const s = {A()};. I do get that error from both the CFE and the analyzer. I think it's fine!
chloestefantsova
changed the title
CFE accepts objects with non-primitive equality as constants
CFE accepts records with non-primitive equality as elements of const sets
Nov 21, 2022
This is not a problem. A class can have a constant constructor and be used in constant expressions, and still have an operator == that runs any code it wants. However, there should be an error if const A() is used in a situation where it is required to have a primitive operator ==, like const s = {A()};. I do get that error from both the CFE and the analyzer. I think it's fine!
Thanks for the clarification, Erik! I updated the text of the issue accordingly.
Uh oh!
There was an error while loading. Please reload this page.
UPDATE: As mentioned by @eernstg in the comment below, the non-trivial equality of objects should affect the constant sets with such objects, not the ability of the objects to be constant. The original contents of the issue is left at the bottom, for archival purposes.
The following program is compiled by the CFE without any compile-time error.
A compile-time error is expected because
A
declares a non-trivial equality, and records containing such objects can't be elements of constant sets.The analyzer displays the following error message for that program.
error - asdf.dart:7:25 - The type of an element in a constant set can't override the '==' operator, but the type '(A)' does. Try using a different value for the element, or removing the keyword 'const' from the set. - const_set_element_type_implements_equals
THE ORIGINAL ISSUE CONTENTS:
The text was updated successfully, but these errors were encountered: