Respond to an unknown method call: Difference between revisions
(added python) |
(add E example) |
||
Line 3: | Line 3: | ||
This task is intended only for object systems that use a dynamic dispatch mechanism. |
This task is intended only for object systems that use a dynamic dispatch mechanism. |
||
=={{header|E}}== |
|||
In E, a message consists of a ''verb'' (arbitrary string) and ''arguments'' (sequence of arbitrary objects). It is conceptually entirely up to any given object how it dispatches incoming messages. |
|||
Practically, the object definition syntax provides a ''matcher'' clause to handle unrecognized messages. This example has the same behavior as the Python example. |
|||
<lang e>def example { |
|||
to foo() { println("this is foo") } |
|||
to bar() { println("this is bar") } |
|||
match [verb, args] { |
|||
println(`got unrecognized message $verb`) |
|||
if (args.size() > 0) { |
|||
println(`it had arguments: $args`) |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
Revision as of 00:08, 4 June 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Demonstrate how to make the object respond (sensibly/usefully) to an invocation of a method on it that it does not support through its class definitions. Note that this is not the same as just invoking a defined method whose name is given dynamically; the method named at the point of invocation must not be defined.
This task is intended only for object systems that use a dynamic dispatch mechanism.
E
In E, a message consists of a verb (arbitrary string) and arguments (sequence of arbitrary objects). It is conceptually entirely up to any given object how it dispatches incoming messages.
Practically, the object definition syntax provides a matcher clause to handle unrecognized messages. This example has the same behavior as the Python example.
<lang e>def example {
to foo() { println("this is foo") } to bar() { println("this is bar") } match [verb, args] { println(`got unrecognized message $verb`) if (args.size() > 0) { println(`it had arguments: $args`) } }
}</lang>
Python
Python objects can implement a __getattr__()
method to handle accesses of unknown attributes (methods are just attributes that are callable; so this function handles both methods and non-method fields). Here we assume that if you access an unknown attribute, you want a method, so we return a function that can be called.
<lang python>class Example:
def foo(self): print "this is foo" def bar(self): print "this is bar" def __getattr__(self, name): def method(*args): print "tried to handle unknown method " + name if args: print "it had arguments: " + str(args) return method
example = Example()
example.foo() # prints “this is foo” example.bar() # prints “this is bar” example.grill() # prints “tried to handle unknown method grill” example.ding("dong") # prints “tried to handle unknown method ding”
# prints “it had arguments: ('dong',)”</lang>
Tcl
<lang tcl># First create a simple, conventional class and object oo::class create Example {
method foo {} { puts "this is foo" } method bar {} { puts "this is bar" }
} Example create example
- Modify the object to have a custom ‘unknown method’ interceptor
oo::objdefine example {
method unknown {name args} { puts "tried to handle unknown method \"$name\"" if {[llength $args]} { puts "it had arguments: $args" } }
}
- Show off what we can now do...
example foo; # prints “this is foo” example bar; # prints “this is bar” example grill; # prints “tried to handle unknown method "grill"” example ding dong; # prints “tried to handle unknown method "ding"”
# prints “it had arguments: dong”</lang>