Ruby has method_missing
, Python has getattr
. Does Boo offer something I can use to intercept method calls?
How can I intercept a method call in Boo?
Asked Answered
Yes, Boo has IQuackFu.
Basically, you implement IQuackFu
, which has three methods:
QuackGet
: gets called when you get a property valueQuackSet
gets called when you set a property valueQuackInvoke
: gets called when you invoke a method
Here's an example.
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. –
E
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. –
Excruciate
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. –
Skylight
© 2022 - 2024 — McMap. All rights reserved.