5 ms·
I can’t speak for Ocaml, but in Elm, an if expression as well as a function must return the same type. Therefore the code you posted would be invalid.
by roebk 6y ago
I can’t speak for Ocaml, but in Elm, an if expression as well as a function must return the same type. Therefore the code you posted would be invalid.
- 1-more 6y agoTo expand slightly, the Elm compiler will help you out here and show you what you need to fix. Given this function body randomReturner n = if n < 0.5 then 123 else "abc" The compiler will give you the following info: The 2nd branch of this `if` does not match all the previous branches: 22| if n < 0.5 then 23| 123 24| else 25| "abc" ^^^^^ The 2nd branch is a string of type: String But all the previous branches result in: number Hint: All branches in an `if` must produce the same type of values. This way, no matter which branch we take, the result is always a consistent shape. Read <https://elm-lang.org/0.19.1/custom-types> to learn how to “mix” types. Hint: Try using String.toInt to convert it to an integer? You can try it out on Ellie here: https://ellie-app.com/9nXxfKSStG4a1 https://ellie-app.com/9nXxfKSStG4a1