Stream: beginners

Topic: How to use recursive opaque types?


view this post on Zulip Qqwy / Marten (Jul 08 2022 at 15:34):

I think I'm doing something wrong while constructing an opaque type.

The type definition:

JsonValue := [
  JsonNull,
  JsonBool Bool,
  JsonNum F64,
  JsonStr Str,
  JsonArray (List JsonValue),
  JsonDict (List [Pair Str JsonValue]),
]

I have a JsonArray (List JsonValue) and I want to wrap it so it becomes a JsonValue itself.

── TYPE MISMATCH ──────────────────────────────── examples/json/ParserJson.roc 

The 2nd argument to map is not what I expect:

66      |> map (\res -> @JsonValue res)
                 ^^^^^^^^^^^^^^^^^^^^^^

This argument is an anonymous function of type:

    [JsonArray (List a), JsonBool Bool, JsonDict (List [Pair Str a]),
    JsonNull, JsonNum F64, JsonStr Str] as a -> JsonValue

But map needs the 2nd argument to be:

    [JsonArray (List JsonValue)] -> JsonValue

────────────────────────────────────────────────────────────────────────────────

view this post on Zulip Ayaz Hafiz (Jul 08 2022 at 16:01):

what does the rest of your program look like?

view this post on Zulip Ayaz Hafiz (Jul 08 2022 at 16:02):

In particular what is map and what are you feeding into it before the pipeline operator?

view this post on Zulip Qqwy / Marten (Jul 08 2022 at 17:39):

What map is: A function with the signature map : Parser input a, (a -> b) -> Parser input b
(source: https://github.com/rtfeldman/roc/blob/f4c0925b21afcb46fd6bd0315b19ee97d942a8f8/examples/json/ParserCore.roc#L185-L190 )

What I am feeding (types given at every step as I tried very hard to find out why the compiler was unhappy) https://github.com/rtfeldman/roc/blob/f4c0925b21afcb46fd6bd0315b19ee97d942a8f8/examples/json/ParserJson.roc#L52-L66

view this post on Zulip Ayaz Hafiz (Jul 08 2022 at 18:13):

okay I have a patch for this. I'll send it to you in a couple hours.

view this post on Zulip Qqwy / Marten (Jul 08 2022 at 20:30):

Thanks a lot for your hard work! :bow:


Last updated: Jul 06 2025 at 12:14 UTC