jCLOS

jCLOS is a small object system with a metaobject protocol (MOP), similar to the Common Lisp Object System (CLOS). Written by Gregor Kiczales at Xerox PARC, the original implementation is in Scheme. This project is a port of Tiny-CLOS to Java. Comments of the Scheme implementation have been kept if they still applied.

Availability

This package is available via git:

    git clone http://www.foldr.org/~michaelw/projects/jclos.git

The original Scheme code is available from <ftp://ftp.parc.xerox.com/pub/mops/tiny/>.

A Common Lisp implementation of Tiny-CLOS (by Kiczales) can be found at <ftp://ftp.parc.xerox.com:/pub/openimplementations/oopsla93-tutorial/>

Another port to Common Lisp is MW-TINY-CLOS.

Additional information can also be found at <http://community.schemewiki.org/?Tiny-CLOS>.

Usage

Package jclos contains all jCLOS functionality. The main method contains some example code, which creates an object with two slots, an instance, and a method on the print generic function.

// Main
public static void main(String[] args) {
    System.out.println("JCLOS booted.");

    CLOSInstance POS = defineClass(null, null, 
            Arrays.asList(new DirectSlotDefn[]{
                    new DirectSlotDefn(Symbol.intern("x")),
                    new DirectSlotDefn(Symbol.intern("y"))
                }),
            Symbol.intern("<pos>"));
    
    System.out.println(POS);

    CLOSInstance pos = make(POS);
    pos.setSlot(Symbol.intern("x"), 42);
    System.out.println(pos.slot(Symbol.intern("x")));

    CLOSInstance print = makeGeneric();
    addMethod(print,
            makeMethod(Collections.singletonList(OBJECT),
              new StdCallable() {
                  @Override
                  public Object apply(Object[] args) {
                      System.out.println("'" + args[1] + "' is an OBJECT!");
                      return null;
                  }
              }));
    /* ... */
    print.call(POS);
    print.call(pos);
}

(It is probably evident that Java is sorely missing some kind of facility for syntactic abstraction.)

Tiny-CLOS is meant as tool to study the implementation of MOPs, so the "code is the documentation." User documentation is sparse, potential users are expected to be familiar with CLOS and its MOP. Other sources of information are:

Contact

Michael Weber <michaelw@foldr.org>, 2008