« first day (3155 days earlier)      last day (324 days later) » 

1:00 PM
@Stargateur I'm not familiar with how chat works on SO since I never use it. But I wanted to talk to you 1:1 about not using dyn in Rust, if you have a few minutes?
 
well if I can answer happy to help, feel free to write any question here, many people read this chat
 
Essentially one question I have is about error handling. How do you manage errors returned by a library if you don't use dynamic dispatch?
eg: Consider a set of function calls several levels of stack deep. You want to return an error from the deepest level and propagate it up to the top level. How do you to that without dynamic return types
 
I generally use crate for that to help error handling
these crate see error as concrete type and avoid dyn
then there is docs.rs/anyhow/latest/anyhow that is full dyn error but I personally dislike this style
 
 
2 hours later…
3:13 PM
If I understand correctly, `ThisError` groups a set of possible errors into an enumeration, so if several possible errors occur you still have something which is static at compile time.

So, just to dig into this a bit further -

When you write things which handle errors, do you generally aggregate them into one large enum containing all possible error types? I have seen this pattern used elsewhere and it appears to be the alternative do using dynamic dispatch.

How would you typically deal with the following case:
 
@FreelanceConsultant Grouping errors into enumerations is probably the most common and cleanest solution. It may seem a little painful as it means typing a lot of code, unless you use a helper library like thiserror. It's taste dependent though: while I personally used thiserror (and similar crates) in the paste, I prefer today to write my error types myself.
Regarding aggregation, I prefer today to define a lot of error types, usually one per multi-files module. I now try to avoid gigantic error enums and prefer small, focused, error enums
 
@Stargateur To help me understand your background as well, when you started working in Rust (I assume you write Rust code professionally?) did you come from a systems programming background, or a dynamic languages background, or something else?

The reason I ask this is because I am trying to ascertain whether the design choices Rust users are making are based partly on their backgrounds. For example, C++ and Java engineers usually opt for the dynamic dispatch approach. Weirdly enough, the only Python programmer who I know has moved to Rust seem to be using enum, which strikes me as odd sin
 
Having used dynamic language too much is also a good reason to prefer other approaches...
 
@DenysSéguret This is the pattern I see a lot, but it requires you to do one of two things:

Maintain complex and ever-expanding aggregations of different error types in a single enum.

or:

Simplify at each level and group a set of errors emitted by a library you are dependent on into a single variant of some larger enum.

I have been using enums for return values rather than return errors. The idea was appealing to me because it is a compile time static thing. But I have run into problems with ever increasing complexity in maintaining those enums, or having one gargantuan enum to do every
 
I work in both small and very big projects. Having an error enum per functional module scales very well, there's no problem with that and there's no reason for it to be "ever-expanding" unless you have problems in your code design.
 
3:29 PM
Just for interest, what languages were you personally using before Rust?
 
Ahaha. I'm old. I professionally used Pascal, C, Lisp, Smalltalk, PHP, C++, Java, Javascript, Fortran, Go, and I'm sure I forget some
(this makes me have fact based opinions, but it doesn't makes them right)
 
3:58 PM
Ok so a bit of everything - that doesn't provide me with much information - this question is only really relevant to someone such as myself who although I have used Python, JavaScript, etc, primarily my background is C++, so naturally I am looking to understand "here's how I do something in C++, how should it be done in Rust"

For C programmers, the enum is a very obvious thing to do to deal with errors. For Java programmers, the dynamic dispatch approach is much more obvious.

For Python, although my data sample is 1, I am somewhat surprised to see he likes the enumeration approach. @Starg
 
@FreelanceConsultant I think no matter the background of someone, rust is really not design to prefer dyn, rust is clearly design to use enum and struct, I don't really know why they added dyn except to please people wanting to Rust replace C++, but again I will never repeat enough that Rust is not at all a OOP language and so will never replace C++ or java since you quote it
generally I do only one enum error by crate/lib, if your enum become weird I think that either mean you should start split better your project, like make more lib.
using snafu handling error is very easy IMO
 
4:50 PM
The issue with the enum approach is it appears you must compromise between three things:

- losing type information (several error types become one thing)
- having one gargantuan enum containing everything for one library/level
- having many enums which are harder to maintain, since you have to update all of them for every possible function you might return an error from if the error types returned by your functions changes

I haven't used snafu but it seems like it can automate much of this stuff? Maybe that is an easy solution...
 
There's absolutely no need to lose type information with enums. You should see the set of error enums as a tree, just like the set of directories in your source code. You don't have one gigantic list of direct subdirectories, you just refer to the immediately included ones.
As for snafu, yes it's convenient.
The functions in my_module return types like Result<Something, my_module::Error> and my_module::Error is an enum. Personally I often name this error as MyModuleError but that's not necessary.
 
5:16 PM
Here's a small example (others have probably better ones). This is a "global" error file: github.com/Canop/safecloset/blob/main/src/error.rs whose enum refers to external error types and to the core error type of the core module in the same crate: github.com/Canop/safecloset/blob/main/src/core/core_error.rs
 
@FreelanceConsultant to be clear, nobody say to do: fn foo() -> MyErrorEnum we say to do: fn foo() -> Result<Foo, MyErrorEnum>
 
6:13 PM
In that example, what does the structure of the variant look like? For example, for IO, what is the structure of IO? This looks like the "newtype" pattern? Is that correct?
The issue I see here is - and I'm not necessarily saying I don't agree with the approach of enum, this is just something to consider - even with this tree like structure, you still have to convert an error type into something it is not. Then if you actually want to do something with it, you end up following "nodes" in a "tree" just to get down to the "actual" (original) error type.

On the one hand I agree it is quite an elegant solution, because return types become static, on the other hand this is a bit arduous.
 
6:41 PM
As a matter of interest, for those who don't particularly like the dynamic dispatch approach, why is this the case?
 
7:32 PM
@FreelanceConsultant I don't understand what you mean by "[...] convert an error type into something it is not". Denys example shows a rather expressive error declination that also allows for general typing of the enum itself. One can then match on specific variants or default on the whole, or mix variants and default behaviour.
 
If you call a function which returns a (for example) io error from the standard library, you can't return that directly. (Unless you use dynamic dispatch.) What you end up doing is transforming the type into a variant of an enum. Those are not the same thing, hence you have transmuted the type.
Unless I misunderstand the proposal?
 
I'm really just trying to grasp what's going on, thanks for the clarification. now, I feel like I'm going to say something horribly out of left field, but can you not bubble the io errors up when interacting with such code?
For instance, when using File::open()?
unless you mean, literally returning an error instead of a Result
 
 
1 hour later…
8:50 PM
So on that point, if I were to bubble up the error, that would mean the return type is fixed to be the same type. That is "IO error". (Even in part of a result)

I can't write a function which returns an IO error or some other error type without using dynamic dispatch. (If I am wrong about this please show me an example code)
A Result<OkType, ErrorType> has a static compile time type. ErrorType, and OkType, are static types at compile time. That means if ErrorType=IOError, then there is no way for that function to create and return an error of any other type.
 

« first day (3155 days earlier)      last day (324 days later) »