A simple metaobject protocol for packages
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Sat, 22 Apr 2006 13:22:49 +0200
Hi,
One reason why Allegro Common Lisp has introduced modern mode is that with the current default semantics of interning of strings to symbols, integration with the Unicode world is hard. However, modern mode also creates incompatibilities with legacy Common Lisp code.
Here is an attempt to resolve these two issues with a simple metaobject protocol for packages.
The idea is this: Under this proposal, for each package there is a corresponding class that represents this package. Classes that represent packages are in general anonymous (they cannot be found by find-class) and they are subclasses of the default ANSI Common Lisp class 'symbol. So for example, the package "COMMON-LISP" is represented by a different class than the class that represents "COMMON-LISP-USER", and both are different from the class that represents "KEYWORD", and so on. Whenever a new package is defined, a new class representation is implicitly created which is again anonymous and a subclass of the class 'symbol.
The class metaobject that represents a package can be retrieved from a package object via package-class.
Symbols which are interned in a particular package are direct instances of the class that represents that package. Uninterned symbols are direct instances of the class 'symbol.
A function search-symbol is introduced that behaves similar to find-symbol, except that the string that it is passed is understood to be unmodified, as read for example from source code. Search-symbol in turn calls search-symbol-using-class. Here is how search-symbol could be implemented, modulo error handling:
(defun search-symbol (string &optional (package *package*))
(search-symbol-using-class
(package-class (find-package package))
(find-package package)
string))
The function search-symbol-using-class is a generic function with one default method. The default behavior is to turn the string into uppercase and call find-symbol:
(defgeneric search-symbol (package-class package string)
(:method ((package-class symbol) package string)
(find-symbol package (string-upcase string))))
Whenever the reader reads a symbol with an explicit package designator, search-symbol is called with an unchanged string. So for example, cl:symbol leads to a call of (search-symbol "symbol" "CL"). In other words, the first parameter does not depend on the setting of readtable-case.
A corresponding protocol for intern-symbol and intern-symbol-using-class (as a replacement for intern) is probably also needed.
This protocol allows to express the following:
(defpackage :my-package ...)
(defmethod search-symbol-using-class
"Symbols in my-package are not upped."
((package (eql (package-class (find-package :my-package))))
package string)
(find-symbol string package))
(defmethod intern-symbol-using-class
"Symbols in my-package are not upped."
((package (eql (package-class (find-package :my-package))))
package string)
(intern string package))
Another nice side effect of this proposal would be that the following method definition would become legal:
(defmethod some-method ((symbol (eql 'my-symbol)))
...)
....in case 'my-symbol is from one of my packages. Since the packages are of different classes, there is no danger of stepping on each others toes anymore.
This proposal is only a sketch, I have probably overlooked important details, but maybe this sounds useful enough to some people so that it's worthwhile to work this out in more detail.
Pascal
--
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
.
- Follow-Ups:
- Re: A simple metaobject protocol for packages
- From: Pascal Costanza
- Re: A simple metaobject protocol for packages
- From: Frode Vatvedt Fjeld
- Re: A simple metaobject protocol for packages
- From: Christophe Rhodes
- Re: A simple metaobject protocol for packages
- From: Kalle Olavi Niemitalo
- Re: A simple metaobject protocol for packages
- From: joswig
- Re: A simple metaobject protocol for packages
- From: Rainer Joswig
- Re: A simple metaobject protocol for packages
- From: Rainer Joswig
- Re: A simple metaobject protocol for packages
- Prev by Date: Re: LISP and Object Oriented Databases
- Next by Date: Re: A simple metaobject protocol for packages
- Previous by thread: Features that can only be provided by the implementation?
- Next by thread: Re: A simple metaobject protocol for packages
- Index(es):
Relevant Pages
|
Loading