views:

207

answers:

1

Ruby has method_missing , Python has getattr. Does Boo offer something I can use to intercept method calls?

+7  A: 

Yes, Boo has IQuackFu.

Basically, you implement IQuackFu, which has three methods:

  • QuackGet: gets called when you get a property value
  • QuackSet gets called when you set a property value
  • QuackInvoke: gets called when you invoke a method

Here's an example.

Mauricio Scheffer
A small question: can Boo intercept other object's method invocations? Kinda like Groovy does with the metaclass. I'm thinking the answer's no, since it's statically typed, but I'd like to be sure.
Geo
I don't think you can do it the same way as Groovy, but you could use an IQuackFu-enabled class to wrap/decorate another class. Your IQuackFu-enabled class could have a dictionary of methods and properties to implement expando-behavior.
Mauricio Scheffer
Boo isn't truly dynamic like groovy, you can't change the definition of a Type at runtime. If you declare a variable as 'duck' it essentially does compiler magic to turn that into late binding against that object, which will flow through IQuackFu if it's available. There is no actual metaclass in Boo. You would need IronRuby or IronPython to get something more like the metaclass.
justin.m.chase