获取 elm 中函数的类型签名

Getting type signatures for a function in elm

我正在使用 elm 0.18。

假设我有一个函数可以将我匆忙拼凑的一堆东西串起来。它有效,但我不确定它的类型签名是什么,我希望 elm 告诉我(或提示我)那个类型签名。

例如,我使用 graphql 并有一个函数接受 graphql 字符串、解码器(也没有类型签名)和 Cmd Msg,并通过 HttpBuilder 运行它。

graphQLPost graphiql decoder msg =
    HttpBuilder.post (url ++ "api")
        |> HttpBuilder.withStringBody "text/plain" graphiql
        |> HttpBuilder.withExpect (Http.expectJson decoder)
        |> HttpBuilder.send msg

这行得通,但我不知道为什么。我尝试使用类型签名 graphQLPost : String -> Json.Decode.Decoder -> Cmd Msg 来拟合它,但出现错误。

弄清楚 this 类型签名对我来说并不像找到一种通过 elm 诱导它们的方法那么重要。是否有我可以输入 elm-repl 的命令或可以告诉我签名的命令?

Elm REPL 将为您完成此操作:

> import Http
> import HttpBuilder
> type Msg = Msg
> url = "..."
"..." : String
> graphQLPost graphiql decoder msg = \
|     HttpBuilder.post (url ++ "api") \
|         |> HttpBuilder.withStringBody "text/plain" graphiql \
|         |> HttpBuilder.withExpect (Http.expectJson decoder) \
|         |> HttpBuilder.send msg
<function>
    : String
      -> Json.Decode.Decoder a
      -> (Result.Result Http.Error a -> msg)
      -> Platform.Cmd.Cmd msg

当您编写函数并点击 <Enter> 时,它会向您显示签名。在这种情况下,签名是:

graphQLPost : String
      -> Json.Decode.Decoder a
      -> (Result.Result Http.Error a -> msg)
      -> Platform.Cmd.Cmd msg

运行 elm-make--warn 选项将导致编译器建议您在没有类型注释的函数上包含一个类型注释,它会为你复制粘贴进来。

此外,某些编辑器集成(例如 Elm 的 Visual Studio 代码语言扩展)会将此类警告显示为提示图标,您可以单击该图标以自动添加缺少的类型注释。您可以为此设置一个键盘快捷键,这样您的手就不会离开键盘。