Tutorial by Examples

We use type annotations to avoid ambiguity. Type applications can be used for the same purpose. For example x :: Num a => a x = 5 main :: IO () main = print x This code has an ambiguity error. We know that a has a Num instance, and in order to print it we know it needs a Show instance. T...
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# public static T DoNothing<T>(T in) { return in; } To call this function with a float we can do DoNothing(5.0f) or...
The problem with type arguments being implicit becomes obvious once we have more than one. Which order do they come in? const :: a -> b -> a Does writing const @Int mean a is equal to Int, or is it b? In case we explicitly state the type parameters using a forall like const :: forall a b....
Say you're introducing a class of types that have a size in bytes. class SizeOf a where sizeOf :: a -> Int The problem is that the size should be constant for every value of that type. We don't actually want the sizeOf function to depend on a, but only on it's type. Without type applica...

Page 1 of 1