How to deal with # (magic hash) in Haskell data types?
I started to learn about Servant from the Building an API with Servant! tutorial. When I later got interested in Elm I tried to automatically generate Elm code for this Servant API which uses Int64 and I discovered that Int64 is defined as a primitive unboxed Haskell type:
data {-# CTYPE "HsInt64" #-} Int64 = I64# Int#
, which make use of # (magic hashes).
In the generated Elm module the Haskell type Int64 is represented by the following Elm type:
type Int64 =
I64# Int#
, which is not valid Elm syntax:
-- UNEXPECTED SYMBOL /Users/XXX/elm-frontend/src/Derived.elm
I got stuck on this symbol:
14| I64# Int#
^
It is not used for anything in Elm syntax. Try removing it?
I've initially posted a question on Stack Overflow about this. The obvious solution is to simply avoid data types with magic hashes in the Servant API. But, even if Elm may not have an analogue to boxed vs unboxed types, I cannot help wondering if it wouldn't be useful to be able to generate Elm code for arbitrary Servant APIs including data types with magic hashes. In this example, if the ranges of Haskell Int and Elm Int are the same, the magic hashes could simply be removed and the Haskell Int# be represented by Int in Elm:
type Int64 =
I64 Int
There may be more to this topic than I currently understand and there are probably a lot to consider to make this work for all general uses of magic hashes in Haskell data types. Anyway, since I stumbled upon this I thought I should create a GitHub issue.
The correct solution would be to hardcode this type in Elm so that it converts to the relevant type. Unfortunately, you don't have many choices in Elm, as it is JavaScript underneath. AFAIK there is only Int ... As a quick fix, you can provide your own conversions with makeModuleContentWithAlterations.
I mean, hardcode it in elm-bridge, as in here: https://github.com/agrafix/elm-bridge/blob/master/src/Elm/Module.hs#L115-L140