Hi, I'm playing around with Elm 0.17 Repl and I've noticed this:
> String.all Char.isDigit ""
True : Bool
That can't be right, empty String as no digits, it should return False, shouldn't it?
All characters in the empty string are digits. Just as all numbers in the empty list are positive. And all eggs in an empty bag are blue.
There is no error here.
Hmmmm, still does not make sense to me. If you don't have anything within a String or List, I can't see how you can make such a truthful statement about the entirety of it.
Any links to better explanations?
This is not an Elm or even programming question. It's basic mathematical logic. For example, you may want to read this: http://math.stackexchange.com/questions/202452/why-is-predicate-all-as-in-allset-true-if-the-set-is-empty
But it all really boils down to this:
Of course, at the same time it is also true that all eggs in that empty bag are red. And that's _not_ a contradiction. It's how mathematical logic, and indeed natural language logic as well, works.
Other than the above explanation, you can certainly google dozens of equivalent explanations for other programming languages. For example, https://issues.scala-lang.org/browse/SI-8587
Maybe this will help you most: https://en.wikipedia.org/wiki/Vacuous_truth
TL;DR: Explicit checks for empty strings, errors, etc. yield more predictable results.
This is probably helpful: http://package.elm-lang.org/packages/elm-lang/core/latest/String#toInt
The important thing to notice is that if you're checking user input, you want to use Return.withDefault.
It provides a default that catches the error state you're looking for, without too much plumbing.