Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use switch expression to init a const variable #4021

Open
huanghui1998hhh opened this issue Aug 7, 2024 · 2 comments
Open

Allow use switch expression to init a const variable #4021

huanghui1998hhh opened this issue Aug 7, 2024 · 2 comments
Labels
enhanced-const Requests or proposals about enhanced constant expressions patterns Issues related to pattern matching. request Requests to resolve a particular developer problem

Comments

@huanghui1998hhh
Copy link

huanghui1998hhh commented Aug 7, 2024

Example:

const fooNumber = int.fromEnvironment('NUMBER');

const fooString = switch (fooNumber) {
  1 => 'one',
  2 => 'two',
  3 => 'three',
  4 => 'four',
  _ => 'other',
}; //error

const fooString = fooNumber == 1
    ? 'one'
    : fooNumber == 2
        ? 'two'
        : fooNumber == 3
            ? 'three'
            : fooNumber == 4
                ? 'four'
                : 'other';

We have to nested ternary expression, it's terrible.

@dart-github-bot
Copy link

Summary: The user requests the ability to initialize a const variable using a switch expression, which is currently not allowed. This would simplify code compared to nested ternary expressions.

@lrhn
Copy link
Member

lrhn commented Aug 7, 2024

This introduces the concept of a constant switch expression, which means constant patterns.
Not all patterns can be constant, but some can:

  • _ obviously.
  • Constant patterns with constant values that have primitive equality.
  • Relational patterns on integers, and == patterns, which will be a compile-time error if the switch value doesn't have primitive equality.
  • && and || combiners, ! and as <type> assertions (if the type is constant), ? null-check patterns.
  • Type checking with a constant type. Both TheType() with not field accessors and TheType _ can work.
    • We could even allow String(length: > 0) since string.length is a valid constant expression.
  • Maybe binding patterns. Without destructuring it's going to be an alias for the original value.

Not supported would be:

  • Destructuring (object, map, list) patterns that actually extract something.. Those always require reading properties, which constant evaluation can't.
    • Maybe we can allow record patterns, because we know the property accesses are constant-safe. That makes binding patterns useful.
    • Maybe we can allow {} as an is Map check. Probably better to just require writing Map().
  • Constant patterns with values that do not have primitive equality.

That is it, really. Not bad.

May want to allow record destructuring as a constant operation, so that when we get parameter patterns, you can write a constant constructor like const Point.fromPair(var (int x, int y)) : this.x = x, this.y = y;.

Nothing about this look particularly impossible.
It's just that we generally don't try to expand the constant sub-language much.

@lrhn lrhn transferred this issue from dart-lang/sdk Aug 7, 2024
@lrhn lrhn added request Requests to resolve a particular developer problem patterns Issues related to pattern matching. enhanced-const Requests or proposals about enhanced constant expressions labels Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhanced-const Requests or proposals about enhanced constant expressions patterns Issues related to pattern matching. request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

3 participants