Elm是否具有Haskell的“Read” [英] Does Elm have an equivalent of Haskell's "Read"

查看:138
本文介绍了Elm是否具有Haskell的“Read”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在编写一个在线游戏,我使用Haskell作为服务器端后端,Elm作为前端/渲染。



现在我的GameState作为一个大型ADT,类型很简单,我可以在Elm中使用它。我希望避免使用JSON,并简单地将Elm输出的show输出到数据上,然后我可以将它解析回Elm中的ADT中。

我想知道,是否有任何等同于haskell的read,它可以自动查看show输出的字符串,并将其解析回数据中?如果没有,是否有任何现有的解析器库可用于Elm?



如果我最终使用JSON,有没有办法将它自动​​转换为ADT? (类似于Aeson的FromJSON,也许?)

解决方案

我的理解是,Elm-没有类型类 - 不能轻易拥有 polymorphic 版本的读取 fromJSON 。我也不相信它具有任何良好的泛型编程功能,所以实现类似于派生或OCaml的类似的东西也很难。



不幸的是,这意味着您最好的选择是编写一次性函数来序列化和反序列化您使用的各种类型。您可以在Haskell一侧使用aeson,然后使用将函数写入/从JSON中Elm的JSON库

另一个选择可能是尝试重用Haskell的派生Read 并将其移植到Elm。但是,这可能比它的价值更大,我不确定如何去做。


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?)

解决方案

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.

这篇关于Elm是否具有Haskell的“Read”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆