How to check if the result is “Ok” or “Err in Elm”?

I am trying to check if a string is a valid integer with String.toInt strVar, but I cannot figure out how to translate Resultto Bool.

+4
source share
2 answers

You can match the pattern Result.

If you want to get Bool as output, then for example:

isIntParsable str =
  case String.toInt str of
    Ok _ -> True
    _ -> False
+5
source

Just an update on this. I am using Elm 0.19 and everything has changed a bit. Here is the new code:

isIntParsable str =
  case String.toInt str of
    Just _ -> True
    Nothing -> False

Currently String.toIntreturns Maybe Int.

0
source

Source: https://habr.com/ru/post/1662741/


All Articles