3 ms·
The guarantees in Haskell (and I guess the MLs as well?) come from a much tighter restriction than other languages put on parametric types. For most other lang
by tsimionescu 5y ago
The guarantees in Haskell (and I guess the MLs as well?) come from a much tighter restriction than other languages put on parametric types.
For most other languages that support generics/parametric polymorphism, a type like 'a -> 'a would actually be closer to Typeable a => a -> a in Haskell, which can be an infinity of functions.
That is, in Go I can easily write something like:
func silly[T any](x T) T {
if reflect.TypeOf(x).Kind() == reflect.Int64 {
var y any = int64(17)
return y.(T)
}
return x
}
EDIT: In Java or C#, I can even do it without reflection:
public static <T> T silly(T x) {
if (x instanceof Integer) {
return (T)(Integer)17;
}
return x;
}