3 ms·
Haskell does have overloaded literals: integer literals have type `Num a => a`, and floating-point literals have type `Fractional a => a`. And GHC has an exten
by JoshTriplett 10y ago
Haskell does have overloaded literals: integer literals have type `Num a => a`, and floating-point literals have type `Fractional a => a`. And GHC has an extension OverloadedStrings to make string literals have type `IsString a => a` (which makes it easier to work with ByteString and other different string-like types).
- cousin_it 10y agoAFAIK, Haskell doesn't really have overloading in the sense that other languages use it. "Num a => a" is just one type. Contrast with Java, where a class can have two methods named "foo" with completely different arguments and return types.
- minitech 10y ago“Overloaded literals” only has one meaning as far as I know, and what Haskell has is it: a literal whose type isn’t solely determined by its contents.
- Sean1708 10y agoEssentially the parent commenters are talking about too different things, one is talking about the first bullet point and the other is talking about the third.
- ufo 10y agoHowever, it seems that the really tricky overloads in Swift's case are the lists, which can contain other overloaded literals inside them.
- capitalsigma 10y agoHaskell is also very happy to throw "ambiguous literal..." errors at you when it gets a little confused.
- solidsnack9000 10y agoSorry, I was not being clear. Since I treated "overloaded literals" and "overloading" as separate bullet points I was using "overloading" to mean only overloaded declarations -- declaring the same function multiple times with different types.