3 ms·
Deterministic finalization is another example, with "as hoc solutions" including manually using a finally statement, using an interface and a custom statements
by saurik 9y ago
Deterministic finalization is another example, with "as hoc solutions" including manually using a finally statement, using an interface and a custom statements (such as the with statement from Python and the using statement in C#, or now the try-with-resources statement from recent Java), or syntax like stack allocation (as in C++). And of course error handling in general (not just null value checking), with exceptions being the typical "ad hoc solution". Any time you have pieces of code going together in some boiler-plate fashion, monads are pretty epic, and it is incredibly depressing to see people not really learning this lesson (such as in Rust, which went with all of crazy operators, macros everywhere, and "just do it manually as we don't believe in exceptions but also don't fully understand monads" as their "ad hoc solution" to error handling, when what the language really needs is do syntax to go along with its attempt to rely on the Either monad for all of its errors: monads are largely interesting because of do syntax, which is the payoff for the common abstraction).
- Veedrac 9y agoThere are strong technical reasons why writing a monad abstraction in Rust is particularly hard[1], and to my knowledge nobody has found a way to do so whilst preserving fairly basic properties like being able to use loops or avoid allocations. Using "?" with appropriate traits is much simpler (important for understandability), more globally applicable (important to be general) and preserves ease of important reasoning (eg. of performance). It is certainly not out of ignorance of monads. [1] For example, https://m4rw3r.github.io/rust-and-monad-trait https://m4rw3r.github.io/rust-and-monad-trait
- pjmlp 9y agoUsually someone complains to me that GC FP languages lack deterministic finalization, I end up explaining how to use higher order functions to achieve it. The problem with with, using or similar approaches is that they grew with the language and the majority of them don't allow for trailing closures. More modern languages like Swift or Kotlin make it much nicer to achieve that, making something like using a simple function call.
- saurik 9y agoDoing that with higher-order functions instead of with a monad requires the caller to care about something they could let the type system guarantee. I mean: every single example of using a monad in do donation is just syntax sugar for passing a continuation of the rest of the function to a higher-order function...
- junke 9y agoI don't get how higher order functions help with deterministic finalization in the case of closures, which typically have an unbounded lifetime. How, and when, is the resource captured by a closure disposed of?
- pjmlp 9y agoWhen the closure itself is disposed of, depends on the GC algorithm (RC, tracing, parallel...). My point is about other kinds of resources, namely files, database connections, IPC channels, sockets, native memory buffers, transactions and so forth. The call stack of higher order functions can be used as an arena.
- barrkel 9y agoIf you go all-in with monadic error handling, you end up with code that is isomorphic with and semantically equivalent to checked exceptions. The only difference, in so far as there is a difference, is in how low-level of a primitive you permit to (throw|return Either). For example, do you model floating-point division as returning an Either, since the divisor might be zero - that kind of thing. Exceptions tend to encourage errors from a lower level of primitive.
- saurik 9y agoI agree. ! However, to go "all-in" on monadic error handling requires you to have do notation to abstract away the monad, lest you end up with something (a thing we happen to call Rust ;P) that is much worse than just giving someone checked exceptions.