Re:Strong Types and Their Impact on Testing
by djur on Tuesday November 18, 2014 @08:53PM
(#8627750)
Ah, thanks for the correction.
Re:Strong Types and Their Impact on Testing
by michaelochurch on Tuesday November 18, 2014 @09:24PM
(#8627866)
(Different poster.) In fact, one of the beautiful things about Haskell is that the type system can (usually) infer exactly as much specificity as is needed.
λ> :t 5
5 :: Num a => a
λ> :t 5.6
5.6 :: Fractional a => a
λ> :t (5 :: Integer)
(5 :: Integer) :: Integer
The type of the literal "Num a => a" means that the literal "5" works for any type in the Num typeclass. So you can mix literals with any numeric type because the compiler will correctly infer the right type. You can even get usages like this: λ> :t (\x -> x + 5)
(\x -> x + 5) :: Num a => a -> a
λ> let f = (\x -> x + 5) in (f (0 :: Int), f (0 :: Double))
(5,5.0)
If you tried it with variables you would get a type error:
It sometimes looks like conversion because the literal `1` is polymorphic and can have a more specific type like `Int` or `Double` inferred from context.