[Python-3000] Generic function PEP won't make it in time
Phillip J. Eby
pje at telecommunity.com
Mon Apr 23 18:09:37 CEST 2007
At 04:35 PM 4/23/2007 +0100, Paul Moore wrote:
>If the basic stuff really is that simple, I'd love to see just that
>for now. It would put the advanced stuff into context when it finally
>arrived.
Here you go:
from overloading import overload, abstract
def foo(bar, baz):
"""Default implementation here"""
@overload
def foo(bar:int, baz:str):
"""Overloaded implementation for int/str here"""
@abstract
def spam(fizz):
"""This function has no default implementation, and raises
a "no applicable methods" error if called..."""
@overload
def spam(fizz:list):
"""...unless an appropriate overload is added first"""
Any questions? :)
Miscellaneous notes/points for discussion:
* Names/locations of errors to be raised when methods aren't found or are
ambiguous (e.g. should they be (or subclass) NotImplementedError? TypeError?)
* Notice that it's not necessary to specially declare a function as being
"generic"; you can therefore overload an existing function, classmethod,
staticmethod, etc., and the existing function object is just modified in-place.
* CLOS/AspectJ-style method combination is supported, using @before,
@after, and @around decorators.
That's pretty much it, for the generic function part. The interface part
looks like the "recombinable interfaces" one I previously posted, where you
simply subclass Interface, and you don't have to write any adapter classes,
because the interface is its own adapter class. You just register methods
for stuff.
I would also propose an 'implements' class keyword argument that asks the
interface objects its been given in order to register the class. In the
case of overloading.Interface, that would be done by registering the class'
methods of matching names with the generic functions embedded in the
Interface, but other interface implementations might work differently.
There are perhaps a few more details or features visible at this user
level, but all the
"interesting" stuff (i.e. wizardry and defense against the dark arts) takes
place under the hood. Specifically, how to make all this such that
advanced or customized dispatch systems and interfaces can play in the same
playground, being able to use the *same decorators* and "implements"
keyword, so that systems like PEAK-Rules and zope.interface won't appear to
be second-class citizens.
More information about the Python-3000
mailing list