Last active
September 3, 2025 22:19
-
-
Save bent-rasmussen/be134ced619a93564e61b15a89380b90 to your computer and use it in GitHub Desktop.
No total triple trouble; only double trouble
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Experiment. | |
| // | |
| // If I have a combination of optional types, and want to operate on them | |
| // it is tedious to have to pick the right functions based on precise | |
| // option type, rather than just do a "flatten this value, I don't care | |
| // which combination of Option<_> or ValueOption<_> it is - just make it happen!" | |
| // | |
| // So: | |
| // Mix and match Option<_> and ValueOption<_> | |
| // Results are Option<_> | |
| // | |
| // Sadly Nullable<_> is not in the mix. :-( | |
| // Perhaps one day with static abstract interface members. | |
| module Option2 = | |
| let inline flatten (x: ^Outer) : Option<'T> | |
| when ^Outer : (member IsSome : bool) | |
| and ^Outer : (member Value : ^Inner) | |
| and ^Inner : (member IsSome : bool) | |
| and ^Inner : (member Value : 'T) = | |
| if (^Outer : (member IsSome : bool) x) then | |
| let y = (^Outer : (member Value : ^Inner) x) | |
| if (^Inner : (member IsSome : bool) y) then | |
| Some ((^Inner : (member Value : 'T) y)) | |
| else | |
| None | |
| else | |
| None | |
| let inline map (f: 'T -> 'U) (x: ^O) : Option<'U> | |
| when ^O : (member IsSome : bool) | |
| and ^O : (member Value : 'T) = | |
| if (^O : (member IsSome : bool) x) then | |
| Some (f ((^O : (member Value : 'T) x))) | |
| else | |
| None | |
| let inline bind (f: 'T -> ^R) (x: ^O) : Option<'U> | |
| when ^O : (member IsSome : bool) | |
| and ^O : (member Value : 'T) | |
| and ^R : (member IsSome : bool) | |
| and ^R : (member Value : 'U) = | |
| if (^O : (member IsSome : bool) x) then | |
| let r = f ((^O : (member Value : 'T) x)) | |
| if (^R : (member IsSome : bool) r) then | |
| Some ((^R : (member Value : 'U) r)) | |
| else | |
| None | |
| else | |
| None | |
| let inline defaultValue (def: 'T) (x: ^O) : 'T | |
| when ^O : (member IsSome : bool) | |
| and ^O : (member Value : 'T) = | |
| if (^O : (member IsSome : bool) x) then | |
| (^O : (member Value : 'T) x) | |
| else | |
| def |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment