【发布时间】:2020-06-08 03:29:54
【问题描述】:
我真的很想学习一点 ELM,但我的思想在查询解析时崩溃了,我的想法是创建一个函数来按名称获取查询字符串值,例如:给定查询字符串 ?name=Neuber 一个函数像这样的getParam "name" 会返回Neuber
但它在最基本的例子中失败了,它甚至没有编译
page 来from here
routeParser 来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")
我遇到的错误
-- 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