How to test run elm/url in REPL?


#1

Hi,
I’m just an Elm beginner. And now I’m trying to test elm/url in REPL, I did the following steps:

myParser = s “foo”
myUrl = { fragment = Nothing, host = “foo.com”, path = “#foo”, port_ = Nothing, protocol = Http, query = Nothing }

Now I try this in REPL:
Parser.parse myParser myUrl

And bang, I got infinite types error, no idea what I did wrong, so could anyone please give me a hint? Thank you.


#2

The following steps works in elm repl:

import Url
import Url.Parser as Parser
myParser = Parser.map "GotFoo" (Parser.s "foo")
myUrl = { fragment = Nothing, host = "foo.com", path = "/foo", port_ = Nothing, protocol = Url.Http, query = Nothing }
Parser.parse myParser myUrl

Your myParser = s "foo" is of type Parser a a when parse requires first parameter to be of type Parser (a -> a) a. Basically you need a parser which returns something from successful parse.

For example you can use myParser = Parser.map "GotFoo" (Parser.s "foo") which maps return value you want to actual parser. I used a String value "GotFoo" here to make example simple but usually you would use some custom type instead.

Parser like myParser = Parser.s "foo" </> Parser.int will also work as it has return value. This one parses a path like /foo/123 and then returns that 123.

ps. "#foo" is not valid path, so I fixed that to "/foo" in your example.


#3

Hi malaire, thank you so much, now I understand why my code is not working.


#4

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