Cryptic type error with Url.Parser

I’m trying to extract the first element from the path of a URL using the 0.19 Url.Parser. At first I tried this:

pathHead =
    Url.fromString "https://api.keyvalue.xyz/a17766c6/myKey"
        |> Maybe.andThen (Url.Parser.parse Url.Parser.string)

but that gives me Nothing. So I reasoned that maybe I needed to tell the parser that I was expecting a second element to the path, so I tried…

pathHead =
    Url.fromString "https://api.keyvalue.xyz/a17766c6/myKey"
        |> Maybe.andThen (Url.Parser.parse (Url.Parser.string </> Url.Parser.string))

This fails to compile with a Type Mismatch but then says that the type I gave it is the same as the type it requires?? Specifically:

Type Mismatch
Line 76, Column 52
The 1st argument to `parse` is not what I expect:

76|                |> Maybe.andThen (Url.Parser.parse (Url.Parser.string </> Url.Parser.string))
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This argument is:

    Url.Parser.Parser (String -> String) String

But `parse` needs the 1st argument to be:

    Url.Parser.Parser (String -> String) String

So…

  1. What do I do to actually extract the first element of the path?

  2. Is this a bug in the compiler’s error message generator?

It’s almost there:

pathHead : Maybe String
pathHead =
    Url.fromString "https://api.keyvalue.xyz/a17766c6/myKey"
        |> Maybe.andThen (Url.Parser.parse (Url.Parser.string </> Url.Parser.s "myKey"))

should do the trick. Not sure how to make sense of the compilation error tho…

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