2008-07-12

Methods to Patterns

Explaining patterns is difficult. I suspect that the best way to explain them would be to describe their developed. In this article I’ll touch on the relationship between methods and patterns in the Hula programming language.


In most object-oriented languages the behaviour of an object is referred to as a method. A method can be thought of as a function. What makes a method distinct is how it relates to an object. A method is related to an object through a process known as dispatch. During dispatch the relevant definition is selected using the types of the arguments at hand.


Single Dispatch

Just about all object-oriented languages use single dispatch. In these languages a single argument is used in selecting the relevant definition. This makes writing methods that associate multiple objects a bit tedious.

Pseudo-syntax:

object method(...)


Multiple Dispatch

The more versatile multiple dispatch may be preferable, but is often dismissed as not being object-oriented enough, mainly because of the perceived syntactic requirements of object oriented languages. With multiple dispatch each argument is treated identically; that is, every argument is used in selecting the relevant definition.

Pseudo-syntax:

method(object ...)


Refined Dispatch

With standard dispatch the name of the method is usually specified using an identifier. This separation of identifiers and arguments presents some unfortunate restrictions.

Hula uses a unique refined multiple dispatch, which enables multiple dispatch on discrete objects. During a refined dispatch the identity of the arguments (not the type) is used to select the relevant definition. This allows objects to completely replace identifiers in the language, and is perhaps an order of magnitude more expressive!

Pseudo-syntax:


(method object ...)

or

(object method ...)

etc.

The resulting anonymous properties or behaviours above are referred to as patterns :).


As always if you have any comments, corrections or contributions please feel free to shout.

0 comments: