FUNCTIONS AND METHODS


 

A method is a function which behaviour changes depending on the class of the object it is called upon. For instance, you could decide that the '+' method, if called with 2 numbers computes their sum, and if called with 2 musical structures will concatenate them.

Functions in OM are generic and multivalued.

Generic (or polymorphic) means that a function with arguments A1, A2, ..., An will automatically call a specific method depending on the type of its argument at call time. For example, imagine the function ADD (a1, a2) having two arguments. Consider it to have two different methods :

If you call the function ADD upon two numbers, then it will dispatch automatically to the method ADDnumbers. If you call the function ADD upon two chords then it will dispatch to the method ADDchords. When implementing the method ADDchords you can decide the semantics you would like to give to the "addition" of two chords, e.g. building a new chord which pitches are the set-union of the two arguments pitches. You could as well design other methods for ADD if the arguments are a chord and a number, or any other combination of types.

In OM a type is a class. Thus any built-in or user defined class can be used in the definition of method.

So defining a new function (e.g. ADD) is mostly defining a name. What you'll implement, using the OM visual language, are really its methods.

Multivalued means that a function can yield more than one result. In a patch window, function icons may thus have several outlets (as may patch icon and factory icons).

 

Creating a new function

Your function's icon is now available in the package user (option-click onthe lower half the the user package suitcase inthe package folder).

 

Adding methods to a function.

You have defined so far the new function ADD, and its first method, the method that will be called whenever you call ADD upon two integers. You can check that by dbl-clicking on the function's icon, in the package user. OM displays a function methods window where it appears that the function ADD possesse only one method, and that method is defined for two integers.

Now, in that function methods window, issue the command New Method in the File menu. OM opens a new method definition window. Note that the number of inputs and outputs is now fixed, you'll never be able to change it for ADD, unless you delete the function itself (command Clear inthe File menu with the ADD function icon selected in the Packages window) and redesign it.

Execute again step 3 to step 7, but with Chord class instead of integer class. You'll notice that OM makes all the slots of class chord available just beneath the input icons in the method definition window. So we read the LMidics slots of the chord inputs (list of MidiCents, i.e pitches), we merge them using a set-union function, and we input the new pitch list into the LMidic slot input of a chord factory.

Now we can use the function ADD to "sum" integers as well as chords. We could of cours add new methods for any combination of classes on input.