[ab]use operator overloads for router and layers #1722
alexpyattaev
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
Having used Scala and Haskell quite a bit, who are both into operator overloading, I'm thinking no 😅 By the way did you just read https://reddit.com/r/rust/comments/10golkq/six_fun_things_to_do_with_rust_operator/? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Monitor punching warning, please consider removing sensitive people and children before proceeding
There are currently 3 main operations for constructing routes:
Conceptually, however, both route and nest operations do essentially the same thing, roughly equivalent to
Similarly, "merge" operation is (using a set-theoretic interpretation of the '+' operator) equivalent to
The other thing we commonly do is adding layers, which are, in concept, a pipeline, and can be written as such:
This would also address the somewhat misleading way how layers get executed in reverse order w.r.t. how they are listed in the code.
With some operator overload "magic" one could make all of these a reality.
Obviously, similar syntax may be enabled for the MethodRouter:
The nice bit about this syntax is that one can use 'normal math rules' to expand the statement:
I'm quite certain that with a bit deeper thinking this can be made even less clunky (e.g. using '&' operator for state is, probably, not the only sensible option), would be interesting to know what the others think before I put substantial effort into making it a reality.
There are a few obvious issues with this idea:
PPS: In terms of how hard this is to implement:
nest and route require an intermediate struct, but are still essentially trivial:
Piping layers together would have to construct a ServiceBuilder, which, when piped with Router, would call .layer() on it. So, not hard at all.
Beta Was this translation helpful? Give feedback.
All reactions