开发者

What are the legitimate uses for "method_missing"-type functionality?

Ruby has "method_missing", Tcl has "unknown", and most highly dynamic languages have an equivalent construct that is invoked when an undefined method is called. It makes perfect sense to add such functionality; something needs to happen, and there's no reason not to allow that something to be redefined by the programmer. It's rather trivial to add, and it makes for some neat "check what my language can do" demos.

Where is this behavior actually useful in real application code?

All I can think of is:

  • Maybe useful for starting a debugger without unwinding the stack (but I'm not sure if that would count a开发者_JAVA百科s "regular application code", and an exception would work just as well in most cases).
  • For "magical" proxy objects.. ie a lazy object that gets created or loaded on first use without changing the interface (though this seems pretty easy to do by other means).

Are there other legitimate uses?

Clarification: I don't really consider "syntactical sugar to avoid having to type quotes" to be a legitimate use. Others might, I do not.


Implementing a dynamic Web Service client
http://mpathirage.com/the-importance-of-rubys-method-missing-concept/

  • Adding better debug information on failure,
  • encoding parameters in method name
  • Builders, Accessors, Proxy delegation

    http://olabini.com/blog/2010/04/patterns-of-method-missing/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜