Galen Boyer writes:
> Hi Paul,
>
> I know you recommend eieio highly. Do you have a simple explanation of
> how you use it, along the lines of your simple/elegant description of
> how you connected Elisp to a JVM? (Feel free to forward this to the JDE
> list if you feel it is appropriate for them to read it)
eieio, a packaged created by Eric Ludlum, provides Emacs Lisp with an
object-oriented programming capability. The reasons to use it are the
same as those that apply to Java, C++, etc., e.g., it promotes code
reuse and maintainability. You use eieio in the same way you use any
other object-oriented language. You define classes of objects, create
instances of those objects, set and query their properties, and invoke
their methods as necessary to accomplish the tasks defined by your
application.
eieio provides a set of macros that enable a Lisp program to define classes
of objects and methods that create and manipulate the objects
and access their properties, for example:
;; Define the class of employees
(defclass employee () ;; defclass is an eieio macro
;; employee properties
((name :initarg :name
:type string ;; the value of this property is a string
:initform "John Employee" ;; default value
:documentation "employee's name")
(title :initarg :title
:type string
:documentation "employee's title")
(ext :initarg :ext
:type string
:documentation "phone extension)
(salary :initarg :salary
:type number
"documentation "annual salary"
(manager :initarg :manager
:type employee
:documentation "employee's supervisor"))
"Class of employees")
;; Define a method that operates on employees
(defmethod get-name-of-manager ((this employee))
"Get the name of this employee's manager."
(oref ;; oref is an eieio macro used to reference an object's property
(oref this manager) ;; get the manager of this employee
name)) ;; get the name of the manager
;; Define class of managers
(defclass manager (employee) ;; manager is a subtype of employee
((reports :initarg :reports
:type list
:initform nil
:documentation "employees who report to this manager))
"Class of managers)
;; Define class of companies
(defclass company ()
((name :initarg :name
:type string
:documenation "name of company")
(employees :initarg :employees
:type list
:documentation "list of employees"))
"Class of companies)
;; Define a method that operates on companies
(defmethod add-employee ((this company) name title ext salary )
"Add an employee named NAME with TITLE, EXT, and SALARY to
the list of THIS company's employees.
(let ((new-employee ;; create an instance of an employee object
(employee ;; constructor name is same as that of the class
(concat "employee " name)
:name name
:title title
:ext ext
:salary salary)))
(oset
this
employees
(cons new-employee (oref this employees)))))
Hope this gives you some idea of the simplicity and power of
eieio.
- Paul