[Python-3000] my take on "typeclasses"

Talin talin at acm.org
Thu May 11 23:17:05 CEST 2006


Guido van Rossum <guido <at> python.org> writes:

> The claim should be introspectable. *Conformance* to the claimed API
> may be hard to introspect but IMO the claims should be introspectable,
> separate from whatever we (think we) know about hasattr().

I sense convergence. Woot! :)

Here's a rough sketch of how this might work:

   import concepts

   # Use as an ordinary test
   def serialize( x ):
      if concepts.iterable( x ):
         ...

   # Use as a function argument constraint
   @generic
   def flatten( x:concepts.iterable ):
      ...

   # Another overload
   @generic
   def flatten( x:concepts.associative ):
      ...

   # Default
   @generic
   def flatten( x ):
      ...

'concepts.iterable' is an object that can be used either
to test a claim, or to introspect a claim. If you call it
as a function, it returns True if the input argument satisfies
the constraint. But you can also introspect the concept by
accessing its attributes.

(If you don't like the name 'concepts', another possible name
might be 'traits'.)

'&' and '|' would be overloaded to create a compount
concept:

   concepts.iterable & concepts.associative

would be equivalent to:

   intersection( concepts.iterable, concepts.associative )

where 'intersection' is a predicate class that itself can
be introspected to see what predicates it contains.

You could also mix regular types, so long as one of the
two operators had the overload.
   
Testing for signatures, how about something like this:

   def function( f:concepts.function( concepts.iterable, int ) )

Which describes a function that takes an argument which is
a function that takes an iterable and an int.

-- Talin




More information about the Python-3000 mailing list