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

Sum throw types #1487

Closed
lukepighetti opened this issue Mar 2, 2021 · 2 comments
Closed

Sum throw types #1487

lukepighetti opened this issue Mar 2, 2021 · 2 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@lukepighetti
Copy link

lukepighetti commented Mar 2, 2021

Related: #68 #349 #83

The ability to sum throw types for functions would make errors much more useful in Dart. In practice I see a lot of developers just not handle errors or go to great lengths to catch everything and rethrow them as an uber-error with a string enum and string message.

As I recall, Swift has the ability to sum error types, removing types that have been explicitly caught, and adding rethrow types in their place. This feature might look something like this in dart.

void main() {
  try {
    /// This would have a return type of [String]
    /// and a sum throw type of [BarException] or [BazException]
    final result = throwsBarBaz('');
  } switch (e) {
    case BarException:
      print('bar exception!');
      break;
      
    case BazException:
      print('baz exception!');
      break;
  }
}

/// Return type of [String]
/// Throw type of [FooException] or [BarException]
String throwsFooBar(String e) {
  if (e == 'foo')
    throw FooException();
  else if (e == 'bar')
    throw BarException();
  else
    return e;
}

/// Return type of [String]
/// Throw type of [BarException] or [BazException]
String throwsBarBaz(String e) {
  try {
    final firstPass = throwsFooBar(e);
    if (firstPass == 'baz')
      throw BazException();
    else
      return e;
  } on FooException {
    return 'foo';
  }
}

class FooException {}

class BarException {}

class BazException {}
@lukepighetti lukepighetti added the feature Proposed language feature that solves one or more problems label Mar 2, 2021
@munificent
Copy link
Member

Is the request here a better syntax for catching different types, or to have some kind of static checking of thrown types like checked exceptions? If it's just the former, you can already have multiple on clauses:

void main() {
  try {
    /// This would have a return type of [String]
    /// and a sum throw type of [BarException] or [BazException]
    final result = throwsBarBaz('');
  } on BarException catch (e) {
      print('bar exception!');
  } on BazException catch (e) {
    print('baz exception!');
  }
}

The current syntax is actually even shorter than your proposal. :)

If it's to have static checking of them, well there's #984. But that was closed because I think the world has generally decided that checked exceptions are a bad idea.

@lukepighetti
Copy link
Author

Yes, this is for checked exceptions, so I will close in favor of #984

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

2 participants