Search code examples
type-inferenceelmtype-signature

Getting type signatures for a function in elm


I'm using elm 0.18.

Let's say I have a function that strings together a bunch of stuff that I threw together in a hurry. It works, but I'm not sure what it's type signature is, and I'd like elm to tell me (or hint for me) that type signature.

For example, I use graphql and have a function that takes a graphql string, a decoder (which also doesn't have a type signature), and a Cmd Msg, and runs it through HttpBuilder.

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

This works, though I don't know why. I tried fitting it with the type signature graphQLPost : String -> Json.Decode.Decoder -> Cmd Msg, but I get an error.

Figuring out this type signature is not as important to me as finding a way to induce them through elm. Is there a command that I can enter into elm-repl or something that will tell me the signature?


Solution

  • Elm REPL will do this for you:

    > 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
    

    When you write a function and hit <Enter>, it shows you the signature. In this case the signature is:

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