4 ms·
> To give a super simple example: if in Haskell you see a function with type 'a -> a', you know that it can only be the identify function, because it's generic
by Dylan16807 5y ago
> To give a super simple example: if in Haskell you see a function with type 'a -> a', you know that it can only be the identify function, because it's generic over 'a', so the code can't do anything specific.
So I can't have any conditionals or pattern matching based on a parameter's type, or provide default implementations of any functions?
- simiones 5y agoIn Haskell, you can have conditionals/pattern matching on a parameter's type, in the range of the declared types. For example, if you have a parameter of type `Int | Char`, you can pattern match to see if it's Int or Char. But if it's type is completely unbound, you can't do anything with it except pass it around. There is support for reflection, but you have to explicitly ask for it, using Typeable or Data as far as I understand. That is, your function signature still has to constrain the type parameter to be an instance of Typeable or Data (a "reflectable type").
- eru 5y agoExactly! Thanks for saving me the explanation. And almost any type you care about is an instance of Typeable or Data. And your own types can get those instances for free from the compiler, if you ask for them.
- chowells 5y agoCorrect. And this is good. It's what makes Haskell so powerful. You can look at a type and immediately know a huge number of rules its implementation must follow. And even better, the type is machine checked. So unlike prose documentation, it can never lie to you.