Convert String to symbol (previously `Text.fromString`, like Haskell's `read`)

114 views
Skip to first unread message

Josh Szmajda

unread,
Jan 16, 2017, 6:56:43 PM1/16/17
to Elm Discuss
Hey folks!


I'm looking for a function to convert a String to an internal symbol (signature `String -> a`). Previously this was in `Text.fromString` but apparently that has been removed.

The use-case I have in mind is this router:

```
locationUpdate : Navigation.Location -> App.Msg
locationUpdate location =
  case (String.dropLeft 2 location.hash) of

    "ViewDocList" -> ViewState ViewDocList

    "ViewDocument" -> ViewState ViewDocument

    _ -> App.NoOp

    -- Would rather code this as:
    -- case Text.fromString msg of
    --   Just value -> ViewState value
    --   Nothing    -> App.NoOp
```

In Haskell this would be `read`:

```
Prelude> read "4.5" :: Float
4.5
Prelude> :t read "4.5" :: Float
read "4.5" :: Float :: Float
```

Any ideas on when this might come back?

Thanks!
-Josh

John Kelly

unread,
Jan 16, 2017, 8:32:13 PM1/16/17
to Elm Discuss
Hey! You might want to check this out http://package.elm-lang.org/packages/evancz/url-parser/latest. It's the closest thing to the String -> a type signature you are after. I'm personally not familiar with Haskell's read, so I can't speak much on that. 

Josh Szmajda

unread,
Jan 16, 2017, 9:06:28 PM1/16/17
to Elm Discuss
Yeah that makes sense, it's probably exactly what I want.

Still curious about direct translation of Strings to symbols though :)

Nick H

unread,
Jan 16, 2017, 9:06:50 PM1/16/17
to elm-d...@googlegroups.com
I doubt such a function will be introduced any time soon, because it's not a properly typed operation. In Haskell, you have to add the type annotation to the function call because read's return type is ambiguous. However, Elm doesn't have any similar ability.

For converting Strings to numeric types, there are String.toInt and String.toFloat.

For doing what your example does, I might consider using a dictionary mapping strings to states. Maybe Something like:

viewStates = Dict.fromList [ ("ViewDocList", ViewDocList), ("ViewDocument", ViewDocument) ]

locationUpdate location =
  case Dict.get location viewStates of
    Just state ->
      ViewState state

    Nothing -> 
      App.NoOp

You still need to manually associate strings with view states, but now locationUpdate has the form you want.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Max Goldstein

unread,
Jan 17, 2017, 12:14:02 AM1/17/17
to Elm Discuss
The function String -> a is called eval, and it's not coming to Elm because it would break all kinds of type guarantees. You can implement functions from String to a specific concrete type, although that type will have to have some notion of failure or emptiness if it's not wrapped in a Maybe or a Result. Nick gives you an example of how to do that; more simply you can just do a case analysis of all the strings you want (or if statements with regexes...).

But if at all possible, don't use string identifiers at all. Sometimes it can't be helped (usually JS of JSON interop) but especially if all your union type's tags are not functions, just pass those around within Elm.

Josh Szmajda

unread,
Jan 17, 2017, 12:07:03 PM1/17/17
to Elm Discuss
Ah yes this makes sense, we'd need the explicit type annotation. Has that functionality been discussed? I know I saw a talk where Evan mentioned adding features to the language over time intentionally to help drive adoption.

Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.

Nick H

unread,
Jan 17, 2017, 3:15:52 PM1/17/17
to elm-d...@googlegroups.com
I'm not sure such a feature would have any uses. Right now, all Elm functions have well-typed return values.

Ports are the one place where a type annotation is used to enforce a type at runtime. So I suppose ports are Elm's analog to "read". There has been lots of discussion about improving ports, to translate JavaScript objects into ADTs. It's possible that change might happen at some point.


To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages