Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
Mark Karavan Avatar asked Nov 12 '17 23:11

Mark Karavan


Video Answer


2 Answers

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
like image 78
Chad Gilbert Avatar answered Oct 24 '22 20:10

Chad Gilbert


Running elm-make with the --warn option will cause the compiler to suggest that you include a type annotation on functions that don't have one, and it will provide one for you to copy and paste in.

Also, some editor integrations, such as the Visual Studio Code language extension for Elm, will display these kinds of warnings as a hint icon that you can click to add the missing type annotation automatically. You can set a keyboard shortcut for this to do it without your hands leaving the keyboard.

like image 29
Kevin Yank Avatar answered Oct 24 '22 20:10

Kevin Yank