Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Enumeration Types

Traditionally Lisp-like languages have just used symbols (simple-names in PLOT) to represent enumeration types. However, if you wanted to define enumeration types with type checking, you could easily add support for Java-style enumeration types using macros.

This would allow writing code like:

defenum color
  red
  blue
  green

defenum fancy-color is color
  pink
  turqoise
  teal

defun f(c is color) ....

defun f(i is integer) f(fancy-color(i))

The following macro is sufficient:

defmacro defenum ?classname is name [ ?=is ?superclassname is name ]
                 { ^ ?choice is name }+ =>
  def choices-name = name(classname + "-values", classname)
  def constructor = name("%make-" + classname, classname)
  def indices = range(0, length(choice) - 1)
  def classdef = if superclassname
                    `defclass ?classname constructor: ?constructor(name, index)
                                         is ?superclassname(name, index)`
                  else
                    `defclass ?classname constructor: ?constructor(name, index)
                       name is name = name
                       index is integer = index`
  `do
     ?classdef
     { def ?choice = ?constructor(#?choice, ?indices) & ^ }*
     def ?choices-name = list( { ?choice &, }* )
     def ?classname(index is integer)
       ?choices-name[index]`

This might be done a little differently if PLOT supported metaclasses. The list of enumeration values might be kept in a slot of the class instead of in a separate definition with a conventional name suffixed with "-values".


Previous page   Table of Contents   Next page