[Python-Dev] type categories

David Abrahams dave@boost-consulting.com
Thu, 15 Aug 2002 17:02:04 -0400


From: "Neil Schemenauer" <nas-dated-1029772483.f08c64@python.ca>


> David Abrahams wrote:
> > There's not all that much to what I'm doing. I have a really
simple-minded
> > dispatching scheme which checks each overload in sequence, and takes
the
> > first one which can get a match for all arguments.
>
> Can you explain in more detail how the matching is done?  Wouldn't
> having some kind of type declarations be a precondition to implementing
> multiple dispatch.

Since in Boost.Python we are ultimately wrapping C++ function and member
function pointers, the type declarations are available to us. For each C++
type, any number of from_python converters may be registered with the
system. Each converter can have its own matching criterion. For example,
there is a pre-registered converter for each of the built-in C++ integral
types which checks the source object's tp_int field to decide
convertibility. When you wrap a C++ class, a from_python converter is
registered whose convertibility criterion checks to see if the source
object is one of my extension classes, then asks if it contains a C++
object of the appropriate type. Since we have C++ types corresponding to
some of the built-in Python types (e.g. list, dict, str), the
convertibility criterion for those just checks to see whether the Python
object has the appropriate type. However, we're not limited to matching
precise types: we could easily make a C++ type called "sequence" whose
converter would match any Python sequence (if we could decide exactly what
constitutes a Python sequence <.02 wink>).

HTH,
Dave

P.S. If you want even /more/ gory details, just ask: I have plenty ;-)

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave@boost-consulting.com * http://www.boost-consulting.com