agrafix / agrafix/elm-bridge

Recursive types

Open
#22 6 comments 1 reaction 0 assignees View on GitHub
Dominant language
Elm
Stars
107
Forks
28
PR merge metrics
No merged PRs in 30d

Description

Hi there - there's an issue creating Elm decoders for recursive types, apparently Elm cannot handle recursive values due to it's strictness.

`jsonDecEdit` is defined directly in terms of itself, causing an infinite loop.

Maybe the solution is to give it a dummy argument to a helper function? e.g.

```

jsonDecEdit_ : () -> Json.Decode.Decoder Edit
jsonDecEdit _ = -- real decoder goes here

jsonDecEdit : Json.Decode.Decoder Edit
jsonDecEdit = jsonDecEdit_ ()
```

```
type Edit =
Add Int Object
| Delete Int
| Move (List Int) Vec2
| Multi (List Edit)

jsonDecEdit : Json.Decode.Decoder ( Edit )
jsonDecEdit =
let jsonDecDictEdit = Dict.fromList
[ ("Add", Json.Decode.map2 Add (Json.Decode.index 0 (Json.Decode.int)) (Json.Decode.index 1 (jsonDecObject)))
, ("Delete", Json.Decode.map Delete (Json.Decode.int))
, ("Move", Json.Decode.map2 Move (Json.Decode.index 0 (Json.Decode.list (Json.Decode.int))) (Json.Decode.index 1 (jsonDecVec2)))
, ("Multi", Json.Decode.map Multi (Json.Decode.list (jsonDecEdit)))
]
in decodeSumObjectWithSingleField "Edit" jsonDecDictEdit
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.