CLOS MOP is useful

I'm teaching myself CLOS MOP (Metaobject Protocol for Common Lisp Object System) and I like it.

I want to use it to simplify bindings for cl-gtk2.

With the help of MOP, it's possible to invest a single-time effort to make:

  • class constructor (by specialization the make-instance method).
  • slot accessors (by specializing the slot-value-using-class, slot-boundp-using-class, slot-makunbound-using-class methods)

And after that it will be possible to write code similar to this:

(defclass gtk-label ()
  ((angle :g-property-name "angle"
          :allocation :gobject
          :accessor gtk-label-angle
          :g-property-type "gdouble")
   (label :allocation :gobject
          :g-property-name "label"
          :g-property-type "gchararray"
          :accessor gtk-label-label))
  (:metaclass gobject-class)
  (:g-type-name . "GtkLabel"))

By evaluating this definition, we will have a class that is coupled to the corresponding GObject class:

  • I can create instances of this class. At the same time, this creates the GObject instance with all specified properties filled in:

    (defparameter *label* (make-instance 'gtk-label :angle 15.0 :label "Hello, world!"))
    
  • Properties of a class are accessible as regular Lisp class slots:

    (gtk-label-label *label*)
    =>
    "Hello, world!"
    

It is possible to do all of this without the help of MOP (such as using macros to generate the equivalent code), but with CLOS MOP it turns out to be much more elegant and maintainable.

Now I to find out how generic function metaobjects work and implement GObject subclassing.

And I'm still not sure if MOP if sufficiently supported by all free implementations of Lisp.