ELM QueryString解析器不编译 [英] ELM QueryString parser dont compile

查看:40
本文介绍了ELM QueryString解析器不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在尝试学习ELM,但是在解析查询时我的头脑崩溃了,我的想法是创建一个函数来按名称获取查询字符串值,例如:给定查询字符串?name = Neuber 像这样的函数 getParam name 会返回 Neuber

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

页面来自来自

routeParser 来自从这里开始

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")

我收到的错误

-- 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!


推荐答案

直接的问题是 int页面 会返回也许是Int ,但是您尝试将其与 Result.withDefault一起使用期望 Result 。解决方法是仅使用 Maybe.withDefault

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.

这篇关于ELM QueryString解析器不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆