You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I find myself wanting a version of Report with a well-known base type.
At the moment, as soon as you add some kind of dynamic information (context or source code) to an error you lose information about what the errors could be since everything ends up as Report. This unfortunately reduces us to dynamic downcasting as in most languages with exception-based errors. It's possible to bake in source code information to your errors sometimes, but not always when dealing with error types from other libraries unless you want to re-wrap them all as more cases of your error type (and this gets uglier if error types are parameterized).
What I’m picturing is a Report<E> which is guaranteed to always be downcastable to E in addition to other possible dynamic downcasts. (Sample implementation below).
The tricky part I ran into is that you really want to be able to convert from Report<EFrom> to Report<ETo> so that you are able to convert error types. I don't think this can be accomplished by using the public API while retaining all context. You can downcast to EFrom and convert it, but at that point you've thrown all the context away. Maybe something like this on Context<T,E> would work?
// Locates first error of type E and replaces it with the result of // applying `f`.fnmap_within_context<E,E2>(self,f:F) -> ReportwhereF:FnOnce(E) -> E2,E:Display + Send + Sync + 'static,E2:Display + Send + Sync + 'static;
I’ve mostly worked around this in a way that works nicely by not using Report in the result types at all. This means:
never using context, instead use good errors in the first place
don’t attach source code to the errors, but do attach spans
Point 2 is actually much nicer since in general if you you're parsing something you’re only getting a &str and to incorporate the source code into the error result you need to clone the whole thing (which might be huge). It has actually made my code a lot cleaner to return only errors with SourceSpans inside. This gives the caller the opportunity to create a Report if they want it, so they can do something like:
This has the benefit of not polluting the parsing API with filenames which may or may not be present, and the caller has more control over the ownership of the source so they can move it into the report rather than copying it.
I find myself wanting a version of
Report
with a well-known base type.At the moment, as soon as you add some kind of dynamic information (context or source code) to an error you lose information about what the errors could be since everything ends up as
Report
. This unfortunately reduces us to dynamic downcasting as in most languages with exception-based errors. It's possible to bake in source code information to your errors sometimes, but not always when dealing with error types from other libraries unless you want to re-wrap them all as more cases of your error type (and this gets uglier if error types are parameterized).What I’m picturing is a
Report<E>
which is guaranteed to always be downcastable toE
in addition to other possible dynamic downcasts. (Sample implementation below).The tricky part I ran into is that you really want to be able to convert from
Report<EFrom>
toReport<ETo>
so that you are able to convert error types. I don't think this can be accomplished by using the public API while retaining all context. You can downcast toEFrom
and convert it, but at that point you've thrown all the context away. Maybe something like this onContext<T,E>
would work?Demo of what I attemped:
The text was updated successfully, but these errors were encountered: