tigerswithguitars
tigerswithguitars

Reputation: 2547

Elm try to convert string to option type

Is there any way in Elm to try and convert a string to an option type? Something a bit like...

conversionFunction : String -> MyOption
conversionFunction str = 
    case *SomeConvertionFunction* str of
        Nothing -> DefaulOption
        Just i  -> i

recievingFunction : List String -> List MyOption
recievingFunction list = 
    List.map (conversionFunction) list

I am trying to parse something from a URL, but I can see that happening a lot in the app I am building. I can also see why this might be difficult when data is attached, but quite useful for enumerations. I don't really want to have to write a case statement for all the strings in the enumeration by hand due mostly to laziness.

It seemed like it would be something in the core libraries, but I can't find it.

Upvotes: 2

Views: 807

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

No, there is currently no Elm support for something like Haskell's read function or what could be described as reflection in other languages. In the current state of Elm, you'll need to write your own conversion function.

See this related question from 2013.

Upvotes: 2

Related Questions