gah! I hate the new string syntax

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Thu Mar 22 17:46:14 EST 2001


Fri, 16 Mar 2001 17:27:10 +0100, Alex Martelli <aleaxit at yahoo.com> pisze:

> Non-invasive mods would be a better target -- and I think a
> runtime variation of Haskell typeclasses might be a nice way
> to present such mods.  E.g., suppose that, parallel to the
> existing builtin 'isinstance(object, [class or type object])',
> that just returns 0 or 1, we have in some future Python a
> new builtin:
>     getinstance(object, [typeclass object, or etc etc])
> which returns either None (meaning the required 'cast' is
> not feasible) OR a reference x such that the axiom:
>     x = getinstance(y, z)
>         =>
>     x is None or isinstance(x, z)
> is satisfied, and x 'represents y as a z'

I imagine it differently, although it's perhaps shifted too far from
current Python's practice towards Haskell.

Interfaces (corresponding to Haskell's typeclasses) are objects which
contain attributes (usually functions).  These functions examine
types/classes of arguments they are applied to, and dispatch the
implementation according to registered instances of that interface,
or raise an exception if the combination has not been registered.

An interface also offers querying whether a combination of
types/classes is valid.

This is an alternative to methods having magical names. Instead
of __hash__ method there would be a Hasbable interface. The builtin
function hash is bound to Hashable.hash. A class C can be made hashable
by executing the following:

    instance Hashable(C):
        def hash(obj):
            return obj.spam ^ obj.eggs

It smoothly extends to multiple dispatch:

    instance Collide(Ship, Rock):
        def collide(ship, rock):
            ...

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list