A few reasons: 1. A type signature is kind of like a test, it makes certain that the expected type matches the inferred type. 2. It's documentation, and it helps make the code easier to understand. I now know the type of view, and can use the function, without ever having to look at its implementation. This is very useful once your programs are too large to hold entirely in your head. 3. It's a good habit to get into, because if you're ever writing a library, exported functions need to be annotated with their type. 4. Sometimes the types are more general than you want, and you can limit them with signatures. You might have some function "myDouble x = 2*x". Elm will give this the type (number -> number), but in your application, you might want to restrict it to only work with one of Int or Float.
I'd say (2) is the most important of these. On Thu, Aug 4, 2016 at 3:32 PM, 'Rupert Smith' via Elm Discuss < [email protected]> wrote: > A wee newb question: > > I notice in some examples the types are explicitly stated. For example: > > http://elm-lang.org/examples/clock > > has > > view : Model -> Html Msg > view model = > > Why is this so? Can the compiler not infer this type? Is it just done for > documentation purposes? If I deleted the "view : Model -> Html Msg" would > the code still work? > > -- > You received this message because you are subscribed to the Google Groups > "Elm Discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Elm Discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
