ELM QueryString parser dont compile

生来就可爱ヽ(ⅴ<●) 提交于 2020-06-23 14:07:51

问题


I am really trying to learn a bit of ELM, but my mind collapse at the query parse, my idea was to create a function to get a query string value by name something like: given an query string ?name=Neuber a function like this getParam "name" that would return Neuber

But its failing at most basic example, it doesn't even compile

page comes from here

routeParser comes from here

module Main exposing (..)
-- import Url.Parser exposing (Parser, (</>), (<?>), oneOf, s)
import Url.Parser.Query exposing (int, map, map2, string)

type alias QueryParams =
  { search : Maybe String
  , page : Maybe Int
  }


routeParser : Url.Parser.Query.Parser QueryParams
routeParser = map2 QueryParams (string "search") (int "page")

page : Url.Parser.Query.Parser Int
page = map (Result.withDefault 1) (int "page")

The error i got

-- TYPE MISMATCH ---------------- /a/long/way/to/project/src/Main.elm

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

15| page = map (Result.withDefault 1) (int "page")
                                       ^^^^^^^^^^
This `int` call produces:

    Url.Parser.Query.Parser (Maybe Int)

But `map` needs the 2nd argument to be:

    Url.Parser.Query.Parser (Result x number)

Hint: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!

回答1:


The immediate problem is that int "page" will return a Maybe Int, but you're trying to use it with Result.withDefault, which, as the error message says, expects a Result. The fix for this is just to use Maybe.withDefault instead.



来源:https://stackoverflow.com/questions/62254561/elm-querystring-parser-dont-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!