| Start |
|---|
| Avoiding type annotations We use type annotations to avoid ambiguity. Type applications can be used for the same purpose. For example This code has an ambiguity error. We know that Another solution using type applications would look like this To understand what this means we need to look at the type signature of The function takes one parameter of type The main difference between value parameters and the type parameters is that the latter ones are implicitly provided to functions when we call them. Who provides them? The type inference algorithm! What So to break down the above example |
| Type applications in other languages If you're familiar with languages like Java, C# or C++ and the concept of generics/templates then this comparison might be useful for you. Say we have a generic function in C# To call this function with a In Haskell it's the same, except that the type parameters are not only implicit at call sites but also at definition sites. This can also be made explicit using either Then at the call site we can again either write |
| Order of parameters The problem with type arguments being implicit becomes obvious once we have more than one. Which order do they come in? Does writing If we don't, then the order of variables is from left to right. The first variable to be mentioned is the first type parameter, the second is the second type parameter and so on. What if we want to specify the second type variable, but not the first? We can use a wildcard for the first variable like this The type of this expression is |
| Interaction with ambiguous types Say you're introducing a class of types that have a size in bytes. The problem is that the size should be constant for every value of that type. We don't actually want the Without type applications, the best solution we had was the The purpose of this type is to carry type information, but no value information. Then our class could look like this Now you might be wondering, why not drop the first argument altogether? The type of our function would then just be The problem is type inference. If I write Luckily, the introduction of type applications saves the day! Now we can write |
Read Article
https://develop.horje.com/learn/1434/reference