3 ms·
> To avoid this callback hell, I defined two macros to help: one normal macro and a very complicated procedural macro. Here’s what the procedural macro looks li
by lmm 6y ago
> To avoid this callback hell, I defined two macros to help: one normal macro and a very complicated procedural macro. Here’s what the procedural macro looks like:
> This expands out the lines to use the non-proc macro, which in turn becomes the callback hell mess. The only problem with this scheme is now that the control flow is all wrong:
My instinct is that any sufficiently large project is going to hit a problem like this as soon as they try to do something the Rust devs didn't anticipate, unless and until the Rust people get monads working.
- nine_k 6y agoDo monads need language-level support? I suppose you can make your own quite well. Support in stdlib is another question, but can't e.g. Option and Result be used monadically with little effort? Am I missing something critical?
- lmm 6y agoYou can implement monads yourself if you have higher-kinded types, but Rust doesn't. Without them you can't implement any of the standard helper functions that you need generically (e.g. monadic versions of control flow functions like ifM, whileM), you'd have to implement them by hand for each different monadic type instead. Thus the control flow problems in the #[dehandle]-based code. And if you look at what #[dehandle] is doing, it's blatantly an ad-hoc implementation of do notation for these handle types. It's doing the same thing as async/await, or try!, but in (present-day) Rust everyone implementing a new "context-like" type has to write their own "very complicated procedural macro" to implement a specific variant for that context-like type. (I don't think you necessarily need do notation to write maintainable monadic code - I'm a big fan of railway-oriented programming style i.e. (Kleisli) arrow composition. But of course you can't do that in Rust either, except by writing ad-hoc implementations of all the operators and helper functions for every different type you want to work with).
- imtringued 6y agoYou're right every monad requires its own ad hoc operators in Rust but Error is already "blessed" with the try! macro. I think the irony is that he traded an Error result for an Error-less "Handle" object which required him to write his own macros. The intention to make things better made things much worse. However, API usability itself is not the problem. Although it was just a small portion of the blog: "Custom Wayland protocols aren’t possible" actually showed the real roadblock. It means wlroots-rs has no reason to exist.
- lmm 6y ago> I think the irony is that he traded an Error result for an Error-less "Handle" object which required him to write his own macros. I don't think he could've used Error, because he needed that custom Handle type to have the right ownership/lifecycle behaviour. > Although it was just a small portion of the blog: "Custom Wayland protocols aren’t possible" actually showed the real roadblock. It means wlroots-rs has no reason to exist. But the big concern there is the same problem: an inability to make an API that can be used safely (without massive amounts of custom engineering).
- quotemstr 6y agoA weak pointer approach would have been perfectly acceptable IMHO --- branch predictors being very good and most wlroots calls being low frequency. It seems to me that the author of this library went to tremendous technical trouble and ultimately failed largely because he prematurely optimized.