Currently, for every definition (in this case, function or variable) with explicit type annotation, it is necessary to type the name of the definition twice, in both type annotation and declaration. This is due to Elm’s syntax origins, which was inspired in Haskell’s syntax.
The reasons why in Haskell it is needed to declare redundantly the definition name (in both type annotation and declaration) are the following: (1) it is possible to declare, without an order, some type annotations, and only later its implementations and (2) it has a more complex and not unified syntax (that is, there are, at least, 2 ways of describing type annotations).
For example, this is a valid (but not desirable) Haskell program.
sumAndConvertToStr a b c d = show (a + b + c + d) fooA :: Int -> Int second :: Int (second, fooA, fourth) = (2, \x -> x + 1, 4) third, fourth :: Int third = 3 sumAndConvertToStr :: Int -> Int -> Int -> Int -> String main = putStrLn (sumAndConvertToStr (fooA 2) second third fourth)
The redundancy here is needed to especify what type annotation refers to each implementation. Otherwise in Elm, there is only one way to describe a type annotation and it is obligatory to have an implementation below it.
For example, the same snippet could be translated this way in Elm:
import Html fooA : Int -> Int fooA x = 1 + x second : Int second = 2 third : Int third = 3 fourth : Int fourth = 4 sumAndConvertToStr : Int -> Int -> Int -> Int -> String sumAndConvertToStr a b c d = toString (a + b + c + d) main = Html.text (sumAndConvertToStr (fooA 2) second third fourth)
This way, we could observe that the syntax is redundant, and typing twice can be tedious and error-prone.
It would be more interesting if we had an unique way to describe the type annotation above the implementation, in a way that we do not repeat the definition name. In this context, the Elm snippet could be converted, for example, into this:
import Html @ Int -> Int fooA x = 1 + x @ Int second = 2 @ Int third = 3 @ Int fourth = 4 @ Int -> Int -> Int -> Int -> String sumAndConvertToStr a b c d = toString (a + b + c + d) main = Html.text (sumAndConvertToStr (fooA 2) second third fourth)
@ is just an example. The community could choose the symbol they prefer (i.e.
: or other that is not currently used in Elm’s syntax), but in this context there would be only one way to do it (and then, the problem of the redundancy would be solved).
This is a very simple proposal that would simplify type annotation removing its redundancy in a way that it would be very easy to convert existing code bases. For beginners it would not be harder to learn it, and compilation speed should increase slightly since it would not be necessary anymore to check if there is an implementation with the same name of the type annotation.