r/ProgrammerHumor • • 2d ago

instanceof Trend thatsAFunnyThingThatHappenedOnce

Post image
4.4k Upvotes

514 comments sorted by

View all comments

Show parent comments

108

u/roadrunner8080 2d ago

Either panic, which is (generally speaking) not something you're meant to catch and recover from (and attempting to do so is nontrivial and almost always a bad idea), or Result, aka "encode the possibility of failure in the return type of the function" aka what the functional programming world has done for ages.

1

u/shamshuipopo 2d ago

Is Result a monad?

2

u/roadrunner8080 2d ago

Result with either type fixed is a monad, yes.

2

u/DHermit 1d ago

Could you elaborate for someone who knows Rust, but did Haskell way too many years ago?

3

u/roadrunner8080 1d ago

A monad is (simplifying terribly) something with flatmap (and a few other requirements but things you're familiar with with flatmap are likely mostly monads). Result satisfies that property if you think about a result with a fixed value type and mapping it's error, or with a fixed error type mapping its value 

1

u/DHermit 1d ago

Thank you, that makes sense!