3 ms·
> It would be nice if languages allowed us to overload function names with the exact same parameters but different return types. Haskell's typeclasses kind-of
by masklinn 10y ago
> It would be nice if languages allowed us to overload function names with the exact same parameters but different return types.
Haskell's typeclasses kind-of do that. `read` has type `Read a => String -> a`, so the result's type `a` (which can be inferred) drives which "Read" instance will be used:
Prelude> read "1" :: Int
1
Prelude> read "1" :: Float
1.0
- dllthomas 10y ago> Haskell's typeclasses kind-of do that. You're over-hedging. As your example shows, Haskell's typeclasses exactly do that.
- masklinn 10y agoI'm mostly hedging on the "overloading" part as Haskell doesn't support arbitrary ad-hoc polymorphism in the sense of C++/Java/C# (IIRC the foundational paper on typeclasses is about making ad-hoc polymorphism less ad-hoc). Depending how far into the overloading field nradov is, typeclasses may not match their definition of or requirements for overloading. It's probably more noticeable in Rust than in Haskell.
- dllthomas 10y agoAh, that makes sense. I don't think there's a reasonable way to define "overloading" that excludes typeclasses, but it's true that there are things that can properly be termed "overloading" which typeclasses can't (or shouldn't) do.