# Objects, slots, methods, functions, and arguments

- *slippery chicken* written in Common Lisp Object System (CLOS)
- Object-oriented language.
- Basic building blocks are classes and methods.

----

## Classes and slots

- Class: *blueprint* for a data container holding:
    - specific number of 
    - specific types of data.
- User (or the software) can create any number of *objects* using this
  blueprint.
- Slots: Named sub-containers within a class object where data is stored. 
- Each new object (*instance*) of this class is separate entity with same
  structure. 
    - Identical slots filled with different (or identical) data.

----

### Example of a class definition: `named-object`

- With slots `id`, `tag`, and `data`.

<!-- end list -->

    (defclass named-object ()
      ((id :accessor id :initarg :id :initform nil)
       (tag :accessor tag :initarg :tag :initform nil)
       (data :accessor data :initarg :data :initform nil)))

----

### Making instances of class objects

- Using CLOS's `make-instance` function.
- Pass values to slots using slot accessor names as keyword arguments.
- Or (preferred): Use *slippery-chicken's* `make-` functions (more below).

        (make-instance 'assoc-list :id 'al-object-1 
                       :data '((3 17) (ob bf3) (c4 q)))

----

## Methods

- Actions that can be performed on the data in a class's slots.
    - Generate, manipulate, read and/or retrieve.
- Specific methods can only be used with specific classes.

### Example of method definition: `get-first` 

- Gets first item stored in `data` slot of an `assoc-list` object.

        (defmethod get-first ((al assoc-list))
          (first (data al)))

----

### Example of use of method

- Class object must have already been created.

        (let* ((al-object-1 
                (make-instance 'assoc-list 
                               :id 'al-object-1 
                               :data '((3 17) (ob bf3) (c4 q)))))
           (get-first al-object-1))

        => 
        NAMED-OBJECT: id: 3, tag: NIL, 
        data: 17

----

## Functions and arguments

- Functions: routines that perform a predefined sequence of operations.
- Very similar to methods but not tied to class instances 
     - and therefore not object-oriented 
- Many *slippery chicken* functions use one or more methods. 
- Arguments: Values passed to them before they begin.
    - Either specified by user when function is called.
    - Or by other routines.
    - Some required, some optional.
- Common type of optional *slippery chicken* argument: keyword.
    - Must be named.
    - Name preceded by colon when used.
    - Can be in any order.

----

### Example of function definition

    (defun get-harmonics (fundamental &key (start-at 1) (min-freq 20)
                          (max-freq 20000)) ...

### Example of use with keyword arguments

    (get-harmonics 63 :start-at 2 :max-freq 1010)

    => (126 189 252 315 378 441 504 567 630 693 756 819 882 945 1008)


----

## The make- functions

- Nearly every sc class has a `make-` function.
- Makes it easier to create objects of that class.
- Many have keyword arguments to set most important slots of the class.
    - Usually identical to slot names. 

----

### Example of a make- function

- `rhythm` class has 23 slots. 
- `make-rhythm` has keyword arguments for two of these:
    - `is-rest`
    - `is-tied-to`
    - *(other two keyword arguments not used to set slots.)*

<!-- end list -->

        (defun make-rhythm (rthm &key (is-rest nil) (is-tied-to nil) 
                            (duration nil) (tempo 60.0))

----

### Using the function with one optional keyword argument

        (make-rhythm 16 :is-rest t)

\small

        => 
        RHYTHM: value: 16.000, duration: 0.250, rq: 1/4, is-rest: T, 
                score-rthm: 16.0, undotted-value: 16, num-flags: 2, num-dots: 0, 
                is-tied-to: NIL, is-tied-from: NIL, compound-duration: 0.250, 
                is-grace-note: NIL, needs-new-note: NIL, beam: NIL, bracket: NIL, 
                rqq-note: NIL, rqq-info: NIL, marks: NIL, marks-in-part: NIL, 
                letter-value: 16, tuplet-scaler: 1, grace-note-duration: 0.05
        LINKED-NAMED-OBJECT: previous: NIL, this: NIL, next: NIL
        NAMED-OBJECT: id: 16, tag: NIL, 
        data: 16

\normalsize

----

## make-slippery-chicken

- Used to make object of `slippery-chicken` class.
- Class has 29 slots.
    - Many of which will contain objects of other classes.
    - e.g. `instrument-palette` object, `rthm-seq-palette` object etc.
- Methods of the `slippery-chicken` class:
    - make instances of sub-objects.
    - set, read, or modify the data they contain.
- `make-slippery-chicken` has 25 optional keyword arguments
    - Most directly set data contained in slots of same name.
    - So when talking about these we may say "instrument-palette slot" rather
      than ":instrument-palette keyword".  

----

### Example of simplest call to `make-slippery-chicken`

    (let* ((mini
            (make-slippery-chicken
             '+mini+
             :ensemble '(((vn (violin :midi-channel 1))))
             :set-palette '((1 ((c4 e4 g4))))
             :set-map '((1 (1)))
             :rthm-seq-palette '((1 ((((2 4) q e e))
                                     :pitch-seq-palette ((1 2 3)))))
             :rthm-seq-map '((1 ((vn (1))))))))
       (midi-play mini)
       (cmn-display mini)
       (write-lp-data-for-all mini))
