Skip to content

Generalize constant equality to use primitive equality #1811

Closed

Description

@eernstg

In order for an object o to be an element in a constant set, o must have primitive equality. Similarly, if o is used as case e in a switch statement where e evaluates to o, o must have primitive equality.

In general, we require that an object must have primitive equality in order to be used in a constant expression in a manner where equality must be tested.

However, the exception (for historical reasons) is operator == itself!

This issue is a proposal to generalize the rule about operator == such that it also relies on primitive equality. We would change the language specification from

An expression of the form \code{$e_1$\,==\,$e_2$} is potentially constant
if $e_1$ and $e_2$ are both potentially constant expressions.
It is further constant if both $e_1$ and $e_2$ are constant and
either $e_1$ evaluates to an object that is an instance of
\code{int}, \code{double}, \code{String}, \code{bool} or \code{Null},
or if $e_2$ evaluates to the null object (\ref{null}).

to something like this:

An expression of the form \code{$e_1$\,==\,$e_2$} is potentially constant
if $e_1$ and $e_2$ are both potentially constant expressions.
It is further constant if both $e_1$ and $e_2$ are constant, and
$e_1$ has a primitive operator \lit{==}
(\ref{theOperatorEqualsEquals}),
or $e_2$ evaluates to the null object (\ref{null}).

Note that this proposal implies that enum values can be used as operands to == in a constant expression, cf. #312.

@leafpetersen, @lrhn, @stereotype441, @munificent, @natebosch, @jakemac53, WDYT?

Activity

  1. added
    small-featureA small feature which is relatively cheap to implement.
    on Aug 19, 2021
  2. lrhn commented on Aug 19, 2021

    @lrhn
    Member

    LGTM.

  3. leafpetersen commented on Aug 19, 2021

    @leafpetersen
    Member

    This also seems fine to me.

  4. AlexV525 commented on Dec 22, 2021

    @AlexV525

    This is blocking us to apply restrictions for assertions that are added to the constant constructor, where we don't want users to make such type.

    If it's not going to push recently or it'll be a quite big change, I want to know how should I solve such requirement:

    enum TestEnum { a, b }
    class TestClass {
      const OrderOption({
        this.type = TestEnum.a,
        this.isTrue = false,
      }) : assert(!(type == TestEnum.a && isTrue));
    
      final TestEnum type;
      final bool isTrue;
    }
    
    void test() {
      const test = TestClass(type: TestEnum.b, asc: true); // <- Throws analyze error here.
    }
    
  5. eernstg commented on Dec 22, 2021

    @eernstg
    MemberAuthor

    You should be able to use identical:

    enum TestEnum { a, b }
    
    class TestClass {
      const TestClass({
        this.type = TestEnum.a,
        this.isTrue = false,
      }) : assert(!(identical(type, TestEnum.a) && isTrue));
    
      final TestEnum type;
      final bool isTrue;
    }
    
    void main() {
      const test = TestClass(type: TestEnum.b, isTrue: true);
    }
    
  6. eernstg commented on Feb 1, 2023

    @eernstg
    MemberAuthor

    @munificent, do you expect this one to be implemented now, following dart-lang/sdk#51045? Nobody has mentioned any reason not to do this.

  7. munificent commented on Feb 27, 2023

    @munificent
    Member

    do you expect this one to be implemented now

    I haven't been tracking the implementation status but I do expect it to be implemented at some point in time since we (the language team) accepted the change.

  8. eernstg commented on Feb 28, 2023

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    small-featureA small feature which is relatively cheap to implement.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions