Replies: 8 comments 2 replies
-
The current implementation of Result x;
if(x case Err(:final err)){
switch(err.rootCause().downcast().unwrap()){
case ..:
case ..:
}
} You could also iterate through with Result x;
if(x case Err(:final err)){
switch(err.rootCause().downcastUnchecked()){
case ..:
case ..:
}
} Which may be a slight improvement. That all said, I am open to suggestions on how to improve things. |
Beta Was this translation helpful? Give feedback.
-
Looking at this, could something like this method be added to the anyhow Result type? void onType<T extends Object>(void Function(T error) f) {
final rootCause = rootCause();
if (rootCause is T) {
f(rootCause);
}
} But I think the use for anyhow is at it's core to provide a simpler way to handle a result with the error information being displayed to you externally, eg: logging. So if you need to do something like retry based on error, it's better to use the base type. |
Beta Was this translation helpful? Give feedback.
-
Definitely useful if you want to inspect the root cause declaratively, but I'm struggling to think of an example of when you would want to do that. Usualy when I need to look at the root cause it is because I need to change my control flow based on the type of error, which I don't think that may be useful for. Do you have an example of when that could be useful? I agree with most of what you said. I'd add though that I view anyhow as also allowing you to only care about the type of error when you want to, rather than coloring the code when you don't even know if you'll ever care about the type of error. |
Beta Was this translation helpful? Give feedback.
-
This might be useful. Object? getRootCause() {
if(x case Err(:final err)){
return err.rootCause()._cause;
}
return null;
} Used like Result x;
switch(x.getRootCause()){ // or "if"
} We'd be diverging from the original anyhow though, but I am open to it. Also, I don't like that name, so open to a better suggestion, just don't want it to be |
Beta Was this translation helpful? Give feedback.
-
Tbh I just don't like this because I don't believe it's the purpose of anyhow. It should be used when you don't care about what the error is, just that you can handle it safely without exceptions, adding context to it and logging in a very digestable format. When you need to inspect the errors for something, like getting a value out of it, you should use the base type, no need to complicate it. I am using the anyhow result especially for internal mobile stuff, like remote-config, where it doesnt matter for the user what error happened, so we just log in our crashlytics. When an error matters for the user, we use the base type with a message from the backend in it's fields to show the user. Using the anyhow result for this is harmful, since you are adding uneeded complexity. |
Beta Was this translation helpful? Give feedback.
-
I think anyhow can be useful for situations like this. Result<int,Exception1> func1();
Result<int,Exception2> func2();
Result<int,???> func3() => Result(($) {
func1()[$];
func2()[$];
}); Where In an ideal world everything should be typed. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I was also disappointed by the context acting as result's error when I discovered anyhow. For me at least, the main advantage of having typed errors would be executing some control flow logic as usual, while also being able to attach error details to the flow that might not be desired to be exposed in the result itself. |
Beta Was this translation helpful? Give feedback.
-
I really like the debug context that the anyhow Result type provides, but it lacks the error type and having to unwrap/downcast is pretty clunky. Is it possible to have typed Errors just like the standard Result type?
Beta Was this translation helpful? Give feedback.
All reactions