Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Elm have an equivalent of Haskell's "Read"

I'm currently writing an online game where I use Haskell for the server-side backend and Elm for the frontend/rendering.

Right now I have my GameState as one big ADT, in a type simple enough that I can use it in Elm as well. I was hoping to avoid using JSON, and simply pass Elm the output of "show" on the data, which I could then parse-back into an ADT in Elm.

I'm wondering, is there anything equivalent to haskell's "read" which can automatically look at a string output by show, and parse it back into data? If not, are there any existing parser-libraries available for Elm?

If I do end up going with JSON, is there a way to automatically convert it into an ADT? (Something similar to Aeson's FromJSON, perhaps?)

like image 854
jmite Avatar asked Oct 04 '22 02:10

jmite


1 Answers

My understanding is that Elm—not having typeclasses—cannot easily have a polymorphic version of read or fromJSON. I also do not believe it has any good facilities for generic programming, so implementing something akin to deriving or OCaml's with would be difficult as well.

Unfortunately, this means your best bet is to write one-off functions for serializing and deserializing the various types you use. You could use aeson on the Haskell side and then write functions to/from JSON using Elm's JSON library.

Another option may be to try reusing the code produced by Haskell's deriving Read and porting it to Elm. However, this might be more work than it's worth, and I am not sure how to go about it, exactly.

like image 169
Tikhon Jelvis Avatar answered Oct 06 '22 00:10

Tikhon Jelvis