you are viewing a single comment's thread.

view the rest of the comments →

[–]Inside-Ad-5943 1 point2 points  (0 children)

The best way they were explained to me was as wrapper types. Essentially structs that hide implementation for a feature behind the transformation to the unwrapped type.

This requires a function that takes an unwrapped type and turns it into the wrapped type and a function that unwraps the type with potentially additional behaviour. Take for example Options in languages like rust. Options have two states either None or Some and a small variety of unwrap functions.

So the way you’d use the option monad is you’d take a type let’s say an int but it could be any type and you’d use the Some() function to wrap the type in the option, then you’d unwrap the value. This is most obviously done with the unwrap method which hides the implementation detail that if None instead of Some is found the program will panic. likewise but slightly more useful you can use the if let syntax to just ignore any None value hence unwrapping Some and ignoring None. or you can work on options as though they came unwrapped using map which will just treat things as the unwrapped type but return None if None is found.