Model an Optional Value with Some and None
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Export a generic Option<T>: either { kind: some, value } or { kind: none }. Write some(value), a none constant, map(option, fn), which applies fn to a some value and leaves none alone (without calling fn), and getOrElse(option, fallback), which returns the value or the fallback. Type them so map gives an Option of fn's return type, getOrElse returns the value's type, and a fallback of another type is a type error.
Your code must pass
- Maps a some value
- Leaves none alone without calling fn
- getOrElse returns the value or the fallback
- Has no type errors
- map gives an Option of fn's return type
- getOrElse returns the value's type
- Rejects a fallback of another type