FAQ
- What does CeX aim to change?
To put error types in function signatures, avoiding to write type alias pub type Result<T> = Result<T,Error>;
, mentioned as “wrapping errors” in Rust by example.
- What does CeX NOT change?
CeX requires neither stack unwinding nor “exception safety”, in other words, not to import “true exceptions” from Java/C++.
- What is the difference between “throw” and “rethrow”?
CeX is based on EnumX. Technically speaking, “throw” is into_enum()
and “rethrow” is exchange_into()
. In other words, “throw” does a one-to-many conversion from a plain error to compound error, while “rethrow” does a many-to-many conversion from a compound error to another one.
- Why use the terminologies “exception”/“throws”, since CeX is essentially not “exception” as the one in C++?
The ?
is mentioned as an exception mechanism of Rust in RFC-0243.
And I regard Rust’s exception as an explicit propagation of Err
.
However, I have no strong opinion about naming. Unfortunately into_enumx()
/exchange_into()
are not related to error-handling. If someone got proper names, they will be adopted.
It seems that some people are quite satisfied with “throws” but the others are not. For those who dislike it, a style of writing vanilla Rust is ready for them, looking like fn foo() -> Result<T, Cex<Enum3<E0,E1,E2>>>;
, without using cex!{}
.
- Is it an O(1) syntactic sugar which could be easily done by using macros?
No.
Currently CeX is O(n*n) in compiling while n is the maximum variants it supports. Maybe it can be improved in O(n), but I don’t think it can be O(1). Generally speaking, it is nontrivial to mimic “sum type” without language support.
On the other hand, if users are satisfied with explicit linkages between compound error types, it is something like “public vs friend” in C++’s terminology. You could not say that “public” is a sugar of “friend”, since they are quite different in user experience and serve for different purposes.
CeX does not forbid the using of “friend”, it just provides the “public” mechanism which is missing and can’t be easily done via macros.
- What if Rust does support this?
- Using standard
From
instead of “(re)throw”. (may require type system changes) - More friendly compile error messages
- Less leaking implementation details
- Potentially compile time improvements
- What NOT if Rust does support this?
-
throws
syntax will NOT propagate to fundamental infrastructures such asmap()
.