1

I am implementing a graph data structure that will store arbitrary objects as vertices. I want to define an interface for things like getting the key for an object so that all vertices can have a key. In my mind, this sounds like where I might use an interface in a language like java.

interface Vertex {
  String key (Vertex v);
  // etc...
}

How can I emulate the behaviour of an interface in common lisp?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
gaigepr
  • 905
  • 1
  • 10
  • 17
  • 8
    Just implement a class (like a mixin) to inherit from and define generic functions as you like. CLOS has multiple inheritance. – Rainer Joswig Aug 15 '16 at 19:22

1 Answers1

5

As Rainer Joswig mentioned in the comments, there are no interfaces, in the sense that some languages use them (e.g., Java), so all you need to do is define some generic functions and optionally a mixin class to specify as a superclass of implementing classes.

Graham
  • 7,431
  • 18
  • 59
  • 84
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Something I think is interesting (and not trivial) is to *check* whether a class implements some interface. In a system like CLOS, where methods may be (and usually are in practice) defined separately than classes, such a check more-or-less has to be delayed until the the first instance of the class is made I think. It ought to be easy enough to do some MOPpy thing to do the check however. –  Aug 16 '16 at 16:46
  • @tfb: Generic functions more-or-less turn that concept upside-down. Rather, you mean to ask, does this generic method have a specialization that includes the class? FIND-METHOD answers that question; eg: (find-method #'stream-clear-output '() (list (find-class 'stream)) – BRPocock Aug 16 '16 at 16:55
  • @tfb I think that's the point of using mixin classes, which otherwise don't serve all that much of a purpose (except for default implementations of methods, in which case people would typically call these abstract classes, rather than interfaces). – Joshua Taylor Aug 16 '16 at 16:56
  • 2
    @tfb And, as BRPocock mentions, generic functions do kind of turn things around. A generic function might have methods that are specialized on more than one argument. (In languages where that isn't readily available, this is usually called [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch), and to say that a class implements method doesn't really make much sense: you *have* to ask whether there's a specialized method for some set of arguments, because it's not up to either class individually to define the behavior. – Joshua Taylor Aug 16 '16 at 16:59
  • @tfb In the Wikipedia article on Multiple Dispatch that I linked to in the previous comment, Common Lisp is used as an example, and it's followed by the text, "In the presence of multiple dispatch, the traditional idea of methods as being defined in classes and contained in objects becomes less appealing—each collide-with method there is attached to two different classes, not one. ... [M]ethod invocation looks exactly like ordinary function invocation, and methods are grouped not in classes but in generic functions." – Joshua Taylor Aug 16 '16 at 17:01
  • @BRPocock That's not what I mean to ask: what I mean to ask is whether an instance of class x has (or will have) methods for generic functions a, b, c. I do understand CLOS, quite well. –  Aug 16 '16 at 22:06