3 ms·
> As you use more generics in a statically typed language, you're sacrificing safety Actually, in languages like Haskell, the more generic your type, the more
by wz1000 10y ago
> As you use more generics in a statically typed language, you're sacrificing safety
Actually, in languages like Haskell, the more generic your type, the more "safe" you can expect it to be.
As an example, consider a function that gives you the first element of the tuple you pass to it.
The most generic type of this function is
fst :: (a,b) -> a
However, it can also have the type
fst1 :: (Int, Int) -> Int
Now, you can be sure of the behaviour of fst immediately by looking at its type, but that doesn't hold for fst1
Pretty much the only definition of fst that the compiler will accept is
fst (x,y) = x
However, the compiler will accept all the following definitions of fst1
fst1 (x,y) = x+y
fst1 (x,y) = x*y
fst1 (x,y) = 2^x
fst1 (x,y) = 7
...