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

Support Object Deconstruction or Else Statements #3506

Closed
mcmah309 opened this issue Dec 9, 2023 · 4 comments
Closed

Support Object Deconstruction or Else Statements #3506

mcmah309 opened this issue Dec 9, 2023 · 4 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@mcmah309
Copy link

mcmah309 commented Dec 9, 2023

Given

sealed class ABC {}

final class A implements ABC {
  final int a;

  const A(this.a);
}

final class B implements ABC {
  final int b;

  const B(this.b);
}

final class C implements ABC {
  final int c;

  const C(this.c);
}

class Foo{
  final int bar;
  
  const Foo(this.bar);
}

Dart already supports object deconstruction:

void main(){
    final Foo(:bar) = Foo(1);
    //  can now use "bar"
}

And Dart supports If-Case statements:

void main(){
    ABC abc = A(1);
    if(abc case A(:final a)){
        // can now use "a"
    }
}

Therefore Dart should also support Object Deconstruction or Else Statements:

void main(){
    ABC abc = A(1);
    A(:final a) = abc else {
        // break, continue, return, or throw
    }
    // can now use "a"
}

This is effectively a pattern matching guard clause.

@mcmah309 mcmah309 added the feature Proposed language feature that solves one or more problems label Dec 9, 2023
@mcmah309
Copy link
Author

mcmah309 commented Dec 9, 2023

For reference, from another language: here

@water-mizuu
Copy link

water-mizuu commented Dec 9, 2023

This seems similar to #2703. You can see the discussion there.

@mcmah309
Copy link
Author

mcmah309 commented Dec 10, 2023

Similar, but different. This is specifically for object deconstruction and fits well into the existing syntax and does not require reserving a new keyword like "let". If "if-case" statements are one side of the coin, then "Object Deconstruction or Else" is the other side of the coin.
Example with current syntax:

void main(){
  ABC abc = A(1);
  int a;
  if(abc case A(a:final innerA)){
    a = innerA;
  }
  else {
    return;
  }
  // use a
}

Example with proposed syntax:

void main(){
  ABC abc = A(1);
  A(:final a) = abc else {
    return;
  }
  // use a
}

@munificent
Copy link
Member

I would like to have something like Swift's "guard let" or Rusts "let if" as well. I'm going to merge this issue with #2537, which discusses that more generally without being pinned to a specific syntax.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants