dontexist
dontexist

Reputation: 5642

How do I check if a Result is Ok or Err in Elm?

I'm trying to see if a string is a valid integer using String.toInt strVar, but I can't figure out how to translate the Result to a Bool.

Upvotes: 5

Views: 504

Answers (2)

Rozar Fabien
Rozar Fabien

Reputation: 362

Just an update on this question. I use Elm 0.19 and things have changed a bit. Here the new code:

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

Currently String.toInt returns a Maybe Int.

Upvotes: 1

Tosh
Tosh

Reputation: 36030

You can pattern match the Result.

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

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

Upvotes: 6

Related Questions