A very simple custom type demo …
What are the benefits overMaybe?
Sorry if this question is hard to follow, I’m struggling a bit to phrase things properly!
Unfortunately the two threads I started have closed now, but I’d like to get people’s thoughts about staying sane with Maybe and representing custom types as json in the context of a very specific snippet of code. Here’s the demo and the custom type:
type alias Id =
Int
type alias Entry =
{ id : Id
, text : String
}
type Entries
= NoEntries
| Entries (List Entry)
That took me a while to get right, and I’m still not 100% sold it’s better than Maybe. On line 142 of the demo we’re having to unwrap the custom type — it’s more specific, and it might (as one user mentioned) help keep impossible states impossible, but I imagine I’m still going to have to unwrap/wrap it like line 249 of this more complex example from “Programming Elm”, which uses Maybe.map.
Maybe.map updatePhoto maybePhoto
I guess I could create my own map function, but you don’t get a helper function like Maybe.map when using a custom type. So far it seems some people seem to prefer getting specific with custom types, and others are happy to use Maybe. Is it simply a matter of preference?
I definitely understand custom types usefulness for the “big” view when caseing for NothingThereYet or SomethingThereNow content (such as a json response). But for general checking of whether a data point is there or not, Maybe seems to cover a lot of bases.
The hard part: saving/retrieving as json
I’m also quite thoroughly confused about how to properly encode and decode things. Simple types are quite easy, but when you start combining them it gets harder. It seems that Json.Decode is a lot more extensive than Json.Encode. If anyone has some resources for tutorials/videos I’d be very grateful.
I understand building up basic decoders and using say, map3 to turn a json object into an Elm record, but …
- How do you do it the other way round? Elm record → json object.
- How do you do it with an
Entriestype?Entry [{ id = 0, text = "entry"}]to{"id" : 0, "text" : "entry"}- It seems (below) that
Encode.objectexpects a tuple - Do you need to change your record into a
List tuplefirst?
- How do you combine the individual json strings with other
encodevalues?- I can see how you’d create an encoded list like
"[True, False, True"]… - But most json files are pretty big;
Json.Encodedoesn’t do a good job of explaining how to build up bigger objects and lists.
- I can see how you’d create an encoded list like
Encode.object
[ ( "name", Encode.string "Tom" )
, ( "age", Encode.int 42 )
]
One example of a bigger json file might be:
- A
Personand their entries - Multiple
Persons in the file, each with theirList entryObjects
How on earth would you go about that?
A small note on codecs
I’m generally all for simplicity, so it seems like codec libraries add difficulty that I might not want. How many of you are using them?
TL;DR
So TL;DR I’m finding it difficult to understand the benefits of using a type Custom over a Maybe type, and how one would build up a json file from smaller parts, especially when using a type Custom or Maybe.