views:

251

answers:

15

In Ruby, objects have a handy method called method_missing which allows one to handle method calls for methods that have not even been (explicitly) defined:

Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior. The example below creates a class Roman, which responds to methods with names consisting of roman numerals, returning the corresponding integer values.

class Roman
 def romanToInt(str)
   # ...
 end
 def method_missing(methId)
   str = methId.id2name
   romanToInt(str)
 end
end

r = Roman.new
r.iv      #=> 4
r.xxiii   #=> 23
r.mm      #=> 2000

For example, Ruby on Rails uses this to allow calls to methods such as find_by_my_column_name.

My question is, what other languages support an equivalent to method_missing, and how do you implement the equivalent in your code?

+3  A: 

Objective-C supports the same thing and calls it forwarding.

scompt.com
+3  A: 

PHP objects can be overloaded with the __call special method.

For example:

<?php
class MethodTest {
    public function __call($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling object method '$name' "
             . implode(', ', $arguments). "\n";
    }
}

$obj = new MethodTest;
$obj->runTest('in object context');
?>
tylermac
+1  A: 

Actionscript 3.0 has a Proxy class that can be extended to provide this functionality.

dynamic class MyProxy extends Proxy {
  flash_proxy override function callProperty(name:*, ...rest):* {
    try {
      // custom code here
    }
    catch (e:Error) {
      // respond to error here
    }
}  
tylermac
+1  A: 

I think this comes from Smalltalk.

Gabriel Ščerbák
+6  A: 

Smalltalk has the doesNotUnderstand message, which is probably the original implementation of this idea, given that Smalltalk is one of Ruby's parents. The default implementation displays an error window, but it can be overridden to do something more interesting.

John Topley
Cool! But is there an example anywhere of how you would use this message?
Justin Ethier
"Combined with #become:, which causes two objects to trade places in memory, this capability of #doesNotUnderstand: is very useful for implementing ProxyObjects such as are needed in Object Relational Mapping frameworks" http://c2.com/cgi/wiki?DoesNotUnderstand
Joe Koberg
+3  A: 

Some use cases of method_missing can be implemented in Python using __getattr__ e.g.

class Roman(object):
  def roman_to_int(self, roman):
    # implementation here

  def __getattr__(self, name):
    return self.roman_to_int(name)

Then you can do:

>>> r = Roman()
>>> r.iv
4
mikej
+1  A: 

In Common Lisp, no-applicable-method may be used for this purpose, according to the Common Lisp Hyper Spec:

The generic function no-applicable-method is called when a generic function is invoked and no method on that generic function is applicable. The default method signals an error.

The generic function no-applicable-method is not intended to be called by programmers. Programmers may write methods for it.

So for example:

(defmethod no-applicable-method (gf &rest args)
  ;(error "No applicable method for args:~% ~s~% to ~s" args gf)
  (%error (make-condition 'no-applicable-method :generic-function gf :arguments args) '()
        ;; Go past the anonymous frame to the frame for the caller of the generic function
        (parent-frame (%get-frame-ptr))))
Justin Ethier
+1  A: 

Tcl has something similar. Any time you call any command that can't be found, the procedure unknown will be called. While it's not something you normally use, it can be handy at times.

Bryan Oakley
+1  A: 

perl has AUTOLOAD, though it's not strictly for methods, so you'll need to test.

sreservoir
+2  A: 

In CFML (ColdFusion, Railo, OpenBD), the onMissingMethod() event handler, defined within a component, will receive undefined method calls on that component. The arguments missingMethodName and missingMethodArguments are automatically passed in, allowing dynamic handling of the missing method call. This is the mechanism that facilitated the creation of implicit setter/getter schemes before they began to be built into the various CFML engines.

Ken Redler
+1  A: 

JavaScript has noSuchMethod, but unfortunately this is only supported by Firefox/Spidermonkey.

Here is an example:

wittyProjectName.__noSuchMethod__ = function __noSuchMethod__ (id, args) {
   if (id == 'errorize') {
    wittyProjectName.log("wittyProjectName.errorize has been deprecated.\n" +
                         "Use wittyProjectName.log(message, " +
                         "wittyProjectName.LOGTYPE_ERROR) instead.",
                         this.LOGTYPE_LOG);
    // just act as a wrapper for the newer log method
    args.push(this.LOGTYPE_ERROR);
    this.log.apply(this, args);
  }
}
Justin Ethier
+1  A: 

Boo has IQuackFu - there is already an excellent summary on SO at how-can-i-intercept-a-method-call-in-boo

Here is an example:

class XmlObject(IQuackFu):
_element as XmlElement 

def constructor(element as XmlElement):
    _element = element 

def QuackInvoke(name as string, args as (object)) as object:
    pass # ignored 

def QuackSet(name as string, parameters as (object), value) as object:
    pass # ignored 

def QuackGet(name as string, parameters as (object)) as object:
    elements = _element.SelectNodes(name)
    if elements is not null:
        return XmlObject(elements[0]) if elements.Count == 1
        return XmlObject(e) for e as XmlElement in elements 

override def ToString():
    return _element.InnerText 
Justin Ethier
+1  A: 

I was looking for this before, and found a useful list (quickly being overtaken here) as part of the Merd project on SourceForge.

Matthew Flaschen
+3  A: 

C# now has TryInvokeMember, for dynamic objects (inheriting from DynamicObject)

Matthew Flaschen
DynamicObject even has some more methods in this regard, though TryInvokeMember is the direct equivalent of method_missing. And there's also ExpandoObject, which allows to add properties at runtime. Depending on what you actually want to solve, these may be good ways too.
OregonGhost
+2  A: 

This is accomplished in Lua by setting the __index key of a metatable.

t = {}
meta = {__index = function(_, idx) return function() print(idx) end end}
setmetatable(t, meta)

t.foo()
t.bar()

This code will output:

foo
bar
Mark Rushakoff