# Decoder a -\> a?

**URL:** <https://discourse.elm-lang.org/t/decoder-a-a/3732>\
**Category:** Learn\
**Created:** [June 2, 2019, 9:19pm UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732 "2019-06-02T21:19:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kochab](https://avatars.discourse-cdn.com/v4/letter/k/a88e57/32.png) [@Kochab](https://discourse.elm-lang.org/u/Kochab)\
**Post date:** [June 2, 2019, 9:19pm UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732/1 "2019-06-02T21:19:36Z")

</div>

In decoders there is a pattern that looks like this:  
`(field "id" int)`  
I want to see the resulting value of this immediately because there is another field that I want to set based on the id. How can I do that?

```
participantDecoder : Int -> Decoder Participant
participantDecoder id =
    let
        actualInt =
            field "id" int -- need to turn into actual int

        idsMatch =
            id == actualInt -- Int /= Decoder Int
    in
    D.map2 Participant
        (field "id" int)
        (D.succeed idsMatch)

```

Edit: See Atlewee’s [post](https://discourse.elm-lang.org/t/decoder-a-a/3732/4) for an `andThen` example.

---

<div class="post-metadata">

**Author:** ![Chadtech](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/chadtech/32/609_2.png) [@Chadtech](https://discourse.elm-lang.org/u/Chadtech)\
**Post date:** [June 2, 2019, 9:50pm UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732/2 "2019-06-02T21:50:47Z")

</div>

`map` can do this, but in many complex cases `andThen` will be more useful.

```elm
participantDecoder : Int -> Decoder Participant
participantDecoder id =
    let
        fromId : Int -> Participant
        fromId decodedId =
            (Participant decodedId (decodedId == id))
    in
    field "id" int
        |> D.map fromId

```

---

<div class="post-metadata">

**Author:** ![choonkeat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/choonkeat/32/1555_2.png) [@choonkeat](https://discourse.elm-lang.org/u/choonkeat)\
**Post date:** [June 2, 2019, 11:26pm UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732/3 "2019-06-02T23:26:56Z")

</div>

> [@Chadtech](#):
>
> `map` can do this

indeed my favourite debug trick, appending a

```elm
|> map (Debug.log "here")

```

---

<div class="post-metadata">

**Author:** ![Atlewee](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/atlewee/32/4603_2.png) [@Atlewee](https://discourse.elm-lang.org/u/Atlewee)\
**Post date:** [June 3, 2019, 7:27am UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732/4 "2019-06-03T07:27:18Z")

</div>

You can use something like this : 🙂

```auto
decodeMsg jsValue =
    D.field "id" D.string
        |> D.andThen decodePayload

decodePayload id=
    case id of
        "IntPayload" ->
            ... the entire msg to be decoded here, and id is already deoded and available...

        "StringPayload" ->
            D.field "payload" (D.map ItHasStringPayload D.string)

        _ ->
            D.fail <| "Event with unknown id: " ++ id

```

Edit: I guess for your usecase:

```auto
decodeMsg jsValue =
    D.field "id" D.int
        |> D.andThen (\idAsInt ->
            ... your decoder... (id is available as Int here) 
            )

```

But I would not have any logic in my decoders,  
I would have just decoded it as is and then check if the Id match in my update function.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [June 13, 2019, 7:27am UTC](https://discourse.elm-lang.org/t/decoder-a-a/3732/5 "2019-06-13T07:27:19Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
