【问题标题】:ELM QueryString parser dont compileELM QueryString 解析器不编译
【发布时间】:2020-06-08 03:29:54
【问题描述】:

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

但它在最基本的例子中失败了,它甚至没有编译

pagefrom here

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

我遇到的错误

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

【问题讨论】:

    标签: functional-programming elm


    【解决方案1】:

    直接的问题是int "page" 将返回Maybe Int,但您尝试将它与Result.withDefault 一起使用,正如错误消息所述,它需要Result。解决此问题的方法是改用Maybe.withDefault

    【讨论】:

    • 很抱歉再次打扰你,但这让我发疯了,这些解析让我感到困惑,如果你能在这个问题的“序列”上帮助我,我会很高兴:stackoverflow.com/questions/62264254/…谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2012-01-28
    • 1970-01-01
    • 2015-01-25
    • 2019-06-29
    • 1970-01-01
    相关资源
    最近更新 更多