Tutorial 42 - Creating a generic function I

A general approach


Topics

Defenition of a generic function and how to create it with OpenMusic

 

Functions used

plus and omloop

 

Description

Since OpenMusic is an open environment, we fluently willencounter a wide variety of data types, such as lists, symbols, atoms etc... and many different objects (CHORD, VOICE, POLY, etc...). If we must apply on most of these a general function such as addition, one must define this function for each type of object. In CommonLisp, this function will be called a generic function. This function will have different methods. In this tutorial we will see how to create such a function and its methods.

 

Creating the generic function 'myplus'

 

 

 

 

 

 

The icons on the right side are standard OM icons. The ones on the left are the user's icons that the user may want to add. For this mater you will have to use a resorce editor such as ResEdit and add these icons in the 'WSPL.rsrc' file that you'll find in the current workspace folder (NOTE: please be carefull while using these editors. Always work with a copy !).

 

 

 

 

 

The main difference between functions that we have done uptill now (e.g tutorial 9 ) and a generic function is the type of our arguments. In a generic function we are editing methods who will apply to specific data types. In the editing window, whenever we add an extra input (in our example we will need two inputs) its type will be a universal one applying to any data type. (This type is the lisp 't' standing for true) .

Here we will start and define a method for integers. In order to apply this type, we must open the 'packages' folder on the workspace and drag from the 'kernel->lisp' subfolders the integer class onto our two extra inputs.

Now we will patch these two inputs to the lisp '+' function (this function takes as arguments only two integers).

 

 

 

(+ 5 3)

Once you drag the new function onto a patch, you must double click on it in order to add a new method and open its methods window

In this window will appear all available methods for this function. Here we still have one method and we can clearly notice the data types concerning its inputs.

To add a new method one must select in the 'File' menu 'New Method'

A new editing window will appear with extra buttons on its upper part (we will comment about these later in this tutorial). This will mean that we are editing a new method for our function.

 

We will again drag two new classes (integer and list) from the packages folder as we have already done for our first method.

If you double click on the omloop icon, you will notice that the integer will be added to each element of the list using listloop for the second argument (cf tutorial 14).

 

 

 

 

 

NOTE: If you want to use them in other workspaces, you must copy them in the 'user' folder of these ones.