简体   繁体   English

榆树有相当于Haskell的“读”

[英]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. 我正在编写一个在线游戏,我在服务器端后端使用Haskell,在前端/渲染时使用Elm。

Right now I have my GameState as one big ADT, in a type simple enough that I can use it in Elm as well. 现在我把我的GameState作为一个大的ADT,这个类型很简单,我也可以在Elm中使用它。 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. 我希望避免使用JSON,并简单地将Elm输出“show”输出到数据上,然后我可以将其解析回Elm中的ADT。

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? 我想知道,有没有相当于haskell的“读取”,它可以自动查看show输出的字符串,并将其解析回数据? If not, are there any existing parser-libraries available for Elm? 如果没有,是否有任何现有的解析器库可供Elm使用?

If I do end up going with JSON, is there a way to automatically convert it into an ADT? 如果我最终使用JSON,有没有办法自动将其转换为ADT? (Something similar to Aeson's FromJSON, perhaps?) (或许类似于Aeson的FromJSON?)

My understanding is that Elm—not having typeclasses—cannot easily have a polymorphic version of read or fromJSON . 我的理解是, fromJSON没有类型类 - 不能轻易拥有readfromJSON多态版本。 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. 我也不相信它有泛型编程任何设施很好,所以实现一个类似于deriving或者Ocaml的with将是困难的为好。

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 . 您可以在Haskell端使用aeson,然后使用Elm的JSON库向JSON编写函数。

Another option may be to try reusing the code produced by Haskell's deriving Read and porting it to Elm. 另一种选择可能是尝试重用Haskell deriving Read的代码并将其移植到Elm。 However, this might be more work than it's worth, and I am not sure how to go about it, exactly. 然而,这可能是更多的工作,而不是它的价值,我不确定如何去做,确切地说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM