Python is DOOMED! Again!

Devin Jeanpierre jeanpierreda at gmail.com
Sun Feb 1 12:45:55 EST 2015


On Sun, Feb 1, 2015 at 8:31 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Paul Rubin wrote:
>> It's completely practical: polymorphism and type inference get you the
>> value you want with usually no effort on your part.
>
> But it's the "usually" that bites you.
>
> If I have an arbitrary pointer, and I want to check if it is safe to
> dereference, how do I do it? Surely I'm not expected to write something
> like:
>
> if type(ptr) == A:
>     if ptr != Anil: ...
> if type(ptr) == B:
>     if ptr != Bnil: ...
>
> etc. That would be insane. So how does Haskell do this?

Haskell has different nulls in the same sense Java does: there's one
keyword, whose type varies by context. Unlike Java, there is no way at
all to cast different nulls to different types.  Haskell has return
value polymorphism and generics, so it's very easy for a function to
return values of different types depending on type parameters. So this
isn't even compiler hackery, it's ordinary.

Also, you don't dereference in Haskell, you unpack. Python and Haskell code:

if x is None:
    print("Not found!")
else:
    print x

case x of
    Nothing -> putStrLn "Not found"
    Some y -> putStrLn (show y)

Both of these work whenever x is something that can be null and can be
shown -- in Haskell, that's anything of type Maybe T, where you have
access to a Show implementation for T.  In Python, None is its own
type/value, in Haskell there is an incompatible Nothing for each T.

-- Devin



More information about the Python-list mailing list