Discussion: throw_ret keyword or similar, cause calling function to return to simplify error handling and analysis functions #9536
Replies: 8 comments 38 replies
-
Feels like you want a |
Beta Was this translation helpful? Give feedback.
-
Based on what @siegfriedpammer said, maybe a workable idea is something like this: -introduce a new kind of exception that doesn't get caught by // where return type is `throw_ret<NormalReturnType, ThrowRetReturnType>
public throw_ret<int, MyErrorType> Foo()
{
a = DoSomething();
if (a is horrible)
throw_ret OhNoes;
return a;
}
void MyErrorType MyFunc()
{
a = Foo();
return Ok;
}
//or even
void MyErrorType MyFunc()
{
try {
a = Foo();
}
catch_ret (ret) {
// you don't get to return me...I'll do something instead
DoSomethingBasedOnRet(ret);
}
} The entire point being that |
Beta Was this translation helpful? Give feedback.
-
IMO |
Beta Was this translation helpful? Give feedback.
-
So just talking out loud, and trying to bring this into sharp focus. Currently, you throw a System.Exception. Potentially, it could look like this: class abstract ExceptionBase {
//bunch of exception stuff
}
class Exception : ExceptionBase {
}
class ReturnException<T> : ExceptionBase {
T ret;
} where: So a lot of the feature ends up being syntactic sugar for something I could almost implement today. |
Beta Was this translation helpful? Give feedback.
-
Why are you trying to return an |
Beta Was this translation helpful? Give feedback.
-
Well, how about this: -provide a way to apply a - -allow single statement I think this actually personally gets me 95% of what I want, and may be useful to others. Error Foo()
{
// applies to the entire block
catch (MySpecialException ex) {
return ex.ret
}
// returns an int if successful, otherwise throws MySpecialException
a = DoSomething();
}
// if I want to catch other exception
Error Foo()
{
// applies to the entire block
catch (MySpecialException ex) {
return ex.ret
}
try {
// returns an int if successful, otherwise throws MySpecialException
a = DoSomething();
}
catch (Exception ex) {
if (Exception is MySpecialException)
throw;
}
}
// using the notation I'm suggesting
Error Foo()
{
// applies to the entire block. I think this would be cool
catch (MySpecialException ex) return ex.ret;
try {
// returns an int if successful, otherwise throws MySpecialException
a = DoSomething();
// similar to `a ? b : c` I've wished there was something like this for many years
a > MAX ?> a = MAX;
try Something() catch return Woops;
}
catch (Exception ex) {
Exception is MySpecialException ?> throw;
}
} Pretty much doesn't change anything for anyone. It's just a bit of syntactic sugar. |
Beta Was this translation helpful? Give feedback.
-
I think what you're looking for is called a "non-local return", where you have a One way to implement this is with exceptions, where the non-local return throws a special exception, and the caller catches it, turning that into a return. I think you're asking for some syntactic sugar to make your implementation of this easier. Scala took this route (although their first attempt was deprecated, the new is similar). Another way to implement this is for the compiler to inline the function, such that a |
Beta Was this translation helpful? Give feedback.
-
Having read the thread a few times now, I'm realizing that this proposal is lacking some realistic, valuable, examples. I think these are necessary so that we can:
Right now I'm caught by 1 and 2. The examples are not compelling currently since they're so vague and basic, and the examples given have simple solutions with existing language constructs. As such, there isn't motivation to even consider adding something new to the language. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
edit: link to more refined version...turned into an evolution of exception handling
#9536 (comment)
#9536 (comment)
We use this pattern a lot:
Obviously these are often implemented as extension functions. I'll be happy to provide a working example, but I think you can imagine what we're doing. It's pretty simple, if not maybe a little abusive.
Basically, we have the situation a lot where we have a bunch of stuff we want to do, but if something fails we want to return the error. We find this pattern to be very expressive and very easy to read, but it still has the penalty of an exception. Maybe even more annoying, I have to wrap things in a try-catch and increase my indent level.
Even MORE annoying, is sometimes I have things that actually DO throw exceptions that I don't want to catch, so I can't even use this mechanism without writing even more code, making special exception types, etc.
What I really want to do is:
I would love to have something like a
throw_ret
keyword. It would cause the CALLER of my function to return whatever value I return. So for example, it is currently implemented like this:I'd love to be able to write it like this:
Perhaps it would make sense to decorate the return type somehow to make it clear that this is different:
Or something like that? I don't know, maybe
throw_ret
isn't a good name because it's not throwing anything? It was just the first thing that came to mind. I'd be happy with almost any name.These are extremely simple cases. We find it very powerful to stick some of our more repetitive error checking, complex functions, etc into reusable blocks. It's not even necessarily for errors! Often times it's just an outcome....I analyzed the thing enough to know what the result is, so you can stop now and return the result. But it feels like we either have to do this kind of abusive (and ugly)
ThrowOnError()
thing we've concocted, or we have to litter very simple code with endless if-return statements.I looked around but couldn't really find a similar proposal. I saw some for things like conditional returns, but what I would really find useful is to actually be able to call a function, It's not just syntactic sugar for when to return.
You can imagine how powerful it would be that you can have different classes that you can pick at runtime affect the logic of your software in such a simple way.
Thanks for reading! We've been casually thinking about this for maybe 2 or 3 years now, trying to figure out what we're actually asking for. We think the last version, where the
throw_ret
and normalreturn
can be mixed just kind of feels right.edit: changed to discussion. I misread the guidelines.
Beta Was this translation helpful? Give feedback.
All reactions