Static Typing in Python

Johan Bockgård bojohan+news at dd.chalmers.se
Sat Mar 20 02:41:22 CET 2004


Donn Cave <donn at u.washington.edu> writes:

>   module Main (main) where
>
>   diff a b | a < b = b - a
>            | otherwise = a - b
>
>   main = do
>         putStrLn (show (diff 2 5))
>         putStrLn (show (diff 2.3 5))
>
> $ runhugs num.hs
> 3
> 2.7
>
> Haskell automatically converts between numeric types, to perform
> arithmetic including comparisons.

Haskell has no implicit conversions. What you see is an effect of
having overloaded numeric constants.

Prelude> :type 0 :: Int
0 :: Int
Prelude> :type 0 :: Float
0 :: Float

0 is not an Int here:

Prelude> let x = 0 in 1.0+x
1.0

The type of x is never converted, so this does not work:

Prelude> let x = 0::Int in 1.0+x
ERROR: Illegal Haskell 98 class constraint in inferred type
*** Expression : let {...} in 1.0 + x
*** Type       : Fractional Int => Int

-- 
Johan Bockgård



More information about the Python-list mailing list