Closed gbassisp closed 6 months ago
This was already discussed and was rejected. Read https://github.com/dart-lang/language/issues/984.
This was already discussed and was rejected.
Thanks for pointing me to that! I looked for a previous request and the only one I found was the one I mentioned. 😞
But I'm glad there was a serious discussion about it and at least there is some more discussion still going on about a possible linter https://github.com/dart-lang/linter/issues/2246
I'm closing this one then
Proposal
In a very short description: a feature similar to the Java
throws
keyword: https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.htmlMotivation
This is actually a counter-proposal to this request here: https://github.com/dart-lang/language/issues/3501
I see this idea of returning a
Success/Failure Result
type coming up often and understand the appeal, but I disagree on this being part of Dart, as pointed out by some people in the comments. I believe this "return error" approach has its value, but belongs in a package to be used at the developer's discretion, and one of the answers here summarizes quite well: "Fortunately Dart already has a solution for that: throwing Exceptions."However, I believe the community will continue to request it because throwing exceptions breaks control flow and, most importantly, many times we don't know what to expect. What kind of errors can you expect from a package you didn't implement? This is where I believe an implementation of
throws
in Dart would outshine Java original approach; Dart's excellent static analysis and type inference would be capable of knowing ahead what could go wrong and where.Syntax
Basically, it would add 2 new keywords:
throws
andnothing
. The latter would be used in the same sense asvoid
, to be explicit about not throwing anything. These keywords would be an optional part of any callable (functions and operators):A simple example of this syntax would be:
Of course, this would lead to verbose function signatures, because every statement can add a new type of exception to be thrown. But this is where type inference would make this feature reasonable, by not requiring to explicitly write it.
The same example with throwable type inference would look like current Dart:
So why does it matter?
So, if it's optional, what would actually change?
raises
keyword that can be used in docstrings and picked up in generated API docs:Example from a python library: https://dateparser.readthedocs.io/en/latest/dateparser.html#dateparser.date.DateDataParser.get_date_data
I don't think this is something we need urgently, but I believe we should think of options that are suitable to Dart and help us plan ahead for the future.
Happy to discuss it further 😄