"multi morphic" classes

pragma eric.t.anderton at gmail.com
Fri May 2 16:19:18 PDT 2008


BCS wrote:
> pragma wrote:
>> That in turn opens the door for a kind of AOP 
> 
> It's neat that it could get that. I had some nebulous thoughts in that 
> direction but don't known enough about it to really comment.
> 
> I think programming for that would result in some interesting code (LOTS 
> of SMALL methods).

Its funny you say that.  If you have some time to kill take a look at 
Python's decorators if you're not already familiar with them:

def hello(fn):
   print "hello"
   fn()

# here the '@' applies hello() as a decorator to the world()
@hello
def world():
   print "world"

When you call world(), you get "hello" "world" as you'd expect.  Of 
course python's decorators are just sugar for the following:

world = hello(world)

... and so forth. Decorators are also allowed to stack ad infinitum.

Unfortunately, this arrangement isn't reflective *at all*, since 
decorators can be applied in any order the developer wishes, and they 
always compose into a call chain.  I once naively expected the '@' to 
work like Java's annotations.  Boy was I in for a surprise.

So with Python you get just like you said: LOTS of SMALL methods.

Anyway, we can do some of this kind of stuff in D already, provided you 
feel like putting up with ugly russian-doll-like template compositions 
on your methods:

void fnImpl(){ /*...*/ }
alias DecoratorA!(DecoratorB!(DecoratorC!(fnImpl))) fn; //yuck!

Runtime stuff, as you illustrated, would involve some delegate wizardry, 
but it's anything but impossible.

- Pragma



More information about the Digitalmars-d mailing list