-
Notifications
You must be signed in to change notification settings - Fork 213
Add Kotlin-like someNullableExpression ?: return
for defensive programming
#2088
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
Comments
Seems like you're essentially asking to allow String? f(String a) {
var b = g(a) ?? return _log("f skip running since b==null");
var c = h(a, b) ?? return _log("f skip running since c==null");
return c;
} For what it's worth, your code can already be made simpler: String? f(String a) {
var b = g(a) ?? _log("f skip running since b==null");
if (b == null) return null;
var c = h(a, b) ?? _log("f skip running since c==null");
if (c == null) return null;
return c;
} |
Seems like #2025. Although I am not against adding this possibility, I hardly disagree that your example is clearer/easier to understand. |
@Levi-Lesches Thanks!
Seems yes!
I may consider that, though I am not very sure whether this shorter code is very readable @mateusfccp Thanks!
Hmm then what about this example:
vs
|
Yeah, this one is better. |
Making Some internals in the compilers might need to change, because they can no longer assume that returns only happen in statements. I have no idea if it's true, but it could easily be true that the internal flow graph doesn't allow outgoing links from expressions. So, likely some necessary and complicated work there. If we do allow expression returns, we should likely also allow |
@irhn Thanks! @Levi-Lesches Thanks I have done it |
Maybe close this issue as well so as to not fill up the issues page? |
When writing Flutter code, we frequently need to do defensive programming - since we never want the user to see crashes. For example:
With the proposed operator, we can simply write down:
which is much simpler to read.
p.s. the
withLogging
is a very simple helper, only define once throughout whole program:The text was updated successfully, but these errors were encountered: