[Python-3000] multi-dispatch again

tomer filiba tomerfiliba at gmail.com
Wed Jan 17 10:25:43 CET 2007


file objects nowadays have ".closed" and ".mode" attributes, but the point
is --
the dispatch mechanism should be able to put constraints on not only on the
*type*, but on the *state* as well -- so once we have multi-dispatch, we
wouldn't need to "manually" check the state of the arguments.

we want to be able to express that in the dispatch itself. imagine this:

def factorial(n):
    if n < 0:
        raise ValueError
    if n < 2:
        return 1
    return n*factorial(n-1)

where i want to write it as

@dispatched
def factorial(n : (lambda n: 0 <= n < 2)):
    return n

@dispatched
def factorial(n : (lambda n: n >= 2)):
    return n * factorial(n-1)

also note that no case is defined for (n < 0), which makes it an exception
automatically. this makes it very much like Haskell's pattern matching.

you can see some more info at http://en.wikipedia.org/wiki/ERights
(look for "guard")


-tomer

On 1/17/07, Bill Janssen <janssen at parc.com> wrote:
>
> Of course, the purpose of using ABCs is to allow easy inspection of
> the capabilities of an object, and for some objects, the state can be
> an important part.  But don't be misled to confuse ABCs with simply
> type-based function dispatch.  They're useful for other things as
> well.
>
> I think that stateful value types should probably have, as you say,
> mechanisms for inspecting that state.  For instance, the current
> "file" type has a "closed" attribute, doesn't it?
>
> Bill
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20070117/02c36fce/attachment.html 


More information about the Python-3000 mailing list