Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explain that map2 takes a constructor function as first argument #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Json/Decode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,31 @@ objects with many fields:

It tries each individual decoder and puts the result together with the `Point`
constructor.

In this example, we use the `Point` type alias because a type alias
generates a record constructor function for the type. `map2` takes as its
first argument a function that constructs the type in question.

Thus if you're working with an opaque type, which unlike a type alias does
not expose a constructor function, you will have to write your own function
that constructs the type, like so:

type Point =
PointConstructor { x: Float, y: Float }

initPoint : Point
initPoint =
PointConstructor {
x : 0
y : 0
}

point : Decoder Point
point =
map2 initPoint
(field "x" float")
(field "y" float")

-}
map2 : (a -> b -> value) -> Decoder a -> Decoder b -> Decoder value
map2 =
Expand Down