Next: Introduction, Previous: Top, Up: Top

## 1 Quick Start

EIEIO provides an Object Oriented layer for Emacs Lisp. You can use EIEIO to create classes, methods for those classes, and instances of classes.

Here is a simple example of a class named `record`, containing three slots named `name`, `birthday`, and `phone`:

         (defclass record () ; No superclasses
           ((name :initarg :name
                  :initform ""
                  :type string
                  :custom string
                  :documentation "The name of a person.")
            (birthday :initarg :birthday
                      :initform "Jan 1, 1970"
                      :custom string
                      :type string
                      :documentation "The person's birthday.")
            (phone :initarg :phone
                   :initform ""
                   :documentation "Phone number."))
           "A single record for tracking people I know.")

Each class can have methods, which are defined like this:

         (defmethod call-record ((rec record) &optional scriptname)
           "Dial the phone for the record REC.
         Execute the program SCRIPTNAME to dial the phone."
           (message "Dialing the phone for %s"  (oref rec name))
           (shell-command (concat (or scriptname "dialphone.sh")
                                  " "
                                  (oref rec phone))))

In this example, the first argument to `call-record` is a list, of the form (varname classname). varname is the name of the variable used for the first argument; classname is the name of the class that is expected as the first argument for this method.

EIEIO dispatches methods based on the type of the first argument. You can have multiple methods with the same name for different classes of object. When the `call-record` method is called, the first argument is examined to determine the class of that argument, and the method matching the input type is then executed.

Once the behavior of a class is defined, you can create a new object of type `record`. Objects are created by calling the constructor. The constructor is a function with the same name as your class which returns a new instance of that class. Here is an example:

         (setq rec (record "Eric" :name "Eric" :birthday "June" :phone "555-5555"))

The first argument is the name given to this instance. Each instance is given a name, so different instances can be easily distinguished when debugging.

It can be a bit repetitive to also have a :name slot. To avoid doing this, it is sometimes handy to use the base class `eieio-named`. See eieio-named.

Calling methods on an object is a lot like calling any function. The first argument should be an object of a class which has had this method defined for it. In this example it would look like this:

         (call-record rec)

or

         (call-record rec "my-call-script")

In these examples, EIEIO automatically examines the class of `rec`, and ensures that the method defined above is called. If `rec` is some other class lacking a `call-record` method, or some other data type, Emacs signals a `no-method-definition` error. Signals.