Skip to end of metadata
Go to start of metadata

Method Missing

The Ruby language has a method_missing mechanism for an object to be notified when a method is invoked but not defined by this object. This mechanism can be useful in senarios such as object-relational mapping.

Jaskell has support for method_missing too. By default, when an undefined tuple member is referenced, Jaskell runtime throws an exception. This behavior can be customized by providing a custom Resolver implementation to the Jaskell runtime.

For example, we could mimic Ruby's method_missing with the following code:

MyResolver.java

And it can be used as:

  • With this MyResolver, Jaskell runtime will call "resolveMember" when an undefined tuple member is referenced.
  • This "resolveMember" will in turn lookup in the tuple for the "method_missing" member.
  • If "method_missing" is not defined, the default value is returned to indicate resolution failure.
  • The member name is passed as parameter to the "method_missing" function if it is a function, otherwise the "method_missing" member itself is returned as the result of the method call.

As you can see, the name "method_missing" in Jaskell is not defined by the language. Rather, it is specified by a Resolver implementation. We could, if we want, use "member_not_found", "method_unknown" etc as the special handler method name. In fact, any action can be taken for method_missing, calling a method with a predefined method name is just one strategy among many.

This allows us to provide different method_missing mechanisms depending on the problem domain being addressed.

Unresolved variables

The Resolver interface can also be used to resolve variables that're not defined in jaskell script. Typically, when you simply type "a+b" in Jaskell shell, you will get an "variable not resolved" error because variable "a" and "b" are used without definition.

This behavior can be customized with a custom Resolver implementation:

VarResolver.java

And it can be used as:

Labels: