Last active
December 17, 2024 08:40
-
-
Save halfAbee/fe5dfcc3559af27e4be9f5d1f2987470 to your computer and use it in GitHub Desktop.
Odin Error Challenge in F#, see: https://rm4n0s.github.io/posts/3-error-handling-challenge/#odins-teachings-in-action
This file contains 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
// Odin Error Challenge in F# | |
// see: https://rm4n0s.github.io/posts/3-error-handling-challenge/#odins-teachings-in-action | |
// try pasting the code into: https://sharplab.io/ | |
type F1_Error = | |
| None | |
| Account_Is_Empty | |
| Investment_Lost | |
type F2_Error = | |
| F1e of F1_Error | |
type F3_Error = | |
| F1e of F1_Error | |
type F4_Error = | |
| F2e of F2_Error | |
| F3e of F3_Error | |
let rnd = System.Random() | |
let f1 () = | |
let n = rnd.Next(9) + 1 | |
if n%2 = 0 then | |
Account_Is_Empty | |
else | |
Investment_Lost | |
let f2 () = | |
F2_Error.F1e (f1()) | |
let f3 () = | |
F3_Error.F1e (f1()) | |
let f4 () = | |
let n = rnd.Next(9) + 1 | |
if n % 2 = 0 then | |
F4_Error.F2e (f2()) | |
else | |
F4_Error.F3e (f3()) | |
let main () = | |
let err = f4() | |
match err with | |
| F2e e2 -> | |
match e2 with | |
| F2_Error.F1e e1 -> | |
match e1 with | |
| Account_Is_Empty -> | |
printfn("Aand it's gone") | |
| _ -> | |
printfn("This line is for bank members only (2)") | |
| F3e e3 -> | |
match e3 with | |
| F3_Error.F1e e1 -> | |
match e1 with | |
| Investment_Lost -> | |
printfn("The money in your account didn't do well") | |
| _ -> | |
printfn("This line is for bank members only (3)") | |
// The %A format specifier is used to format values in a human-readable way, | |
// and can also be useful for reporting diagnostic information. | |
printfn $"%A{err}" | |
// Prints randomly: | |
// | |
// Aand it's gone | |
// F2e (F1e Account_Is_Empty) | |
// | |
// The money in your account didn't do well | |
// F3e (F1e Investment_Lost) | |
// | |
// This line is for bank members only (3) | |
// F3e (F1e Account_Is_Empty) | |
for _ in 1..10 do main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment