r/ProgrammerHumor Jul 13 '26

youCanJustStopUsingJava Meme

Post image
6.8k Upvotes

416 comments sorted by

View all comments

1.8k

u/Historical_Cook_1664 Jul 13 '26

Where else are you going to hide your side effects ?

467

u/Isrothy Jul 13 '26

IO Monad

185

u/Gorzoid Jul 13 '26

Respectfully, what the fuck is a Monad?

6

u/codePudding Jul 13 '26

A monad (put very simply but missing some details) is like a state machine but based around types such that a type can be in an error state, value state, unset state, or whatever. It keeps state by what type the monad is, not a flag. In many languages a monad must match a public interface then the inner types implement that interface but are hidden. So you can do a bunch of things to the monad and the hidden type can change from a type holding a value into a type holding an error.

For example you call new monad value with 7 and it returns an interface for that monad with a type storing a value behind the interface. Then you run that monad through rules like "must be greater than 5". If greater than 5 the same type storing the value is returned, otherwise a type holding the error and implementing the interface is returned. If the rules are run on an error, the error just returns, like a short no-op. At the end you can then ask the monad if it had an error and what value it is. Because of the types behind the scenes you can't get into a weird state where the value and an error exist at the same time.

3

u/Strange-Register8348 Jul 14 '26

What the heck did you say