1

EDIT: I have restructured the question so it is (hopefully) easier to answer. I'm new to CL so it's sometimes hard to describe what I'm trying to do when I'm not even sure what the best way to describe it would be :P

I've been learning some Common Lisp over the past couple weeks and am wondering the best way to create an instance of an object given a configuration file that defines some of the slot values for the class but the values need to be normalized in some form before they are assigned.

So, for a simple example, if I have the following class:

(defclass my-class ()
  ((name
    :initarg :name
    :accessor name)
   (x
    :initarg :x
    :initform 10
    :accessor x)
  (y
   :initarg :y
   :initform nil
   :accessor y)))

and

(defmethod initialize-instance :after ((obj my-class) &key)
  (with-slots (x y)
      obj
    (setf y (* 2 x))))

I would like a way of specifying in an external file, say instance-a.lisp

(possibly-some-macro "A"
    :x 5)

But when constructing an instance the value x must be normalized first some how. The eventual call to make-instance would look something like this:

(make-instance 'my-class 
               :name (name-value-from-config) 
               :x (normalize (x-value-from-config))

Where (name-value-from-config) would be "A" and (x-value-from-config) would be 5. NOTE: These two forms are only here for placeholders to indicate that it should be constructed with the values from the configuration.

My initial thought would be to turn whatever is in the config file into a hash table or a plist with the appropriate keys.

m-renaud
  • 1,275
  • 8
  • 11

1 Answers1

2

It's not clear to me what you really want to do.

Why would one use a macro to create an object?

  • the macro creates compile-time side-effects. For example the object should be available during file compilation.

  • the macro provides a more convenient syntax

But for all other purposes don't try to do something clever. Usually I would just LOAD a Lisp file for configuration.

(defparameter *instance-a*
   (make-instance 'my-class :x 5 :y '(1 2 3)))

If the class needs to be configured:

(defparameter *a-class* 'my-class)

(defparameter *instance-a*
   (make-instance *a-class* :x 5 :y '(1 2 3)))
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • Sorry for the poor wording, I was having a hard time trying to get across what I'm trying to figure out. I was trying to avoid doing that as there would be other stuff in the class and it should only be partially initialized from the config file. Obviously for the simple example class you can just use `make-instance`. I was wondering the best way to handle this. Take a look at asdf:defsystem for example. – m-renaud Jul 05 '13 at 02:16
  • @mrenaud92: you need to come up with a more specific question to get an answer. Pointing to ASDF does not help, since it is a) a large piece of software and b) ASDF is written in Common Lisp in an especially ugly and not idiomatic way. There are a lot of basic design problems and mistakes in ASDF. You may not want to repeat them. Please try to describe a specific use case. – Rainer Joswig Jul 05 '13 at 03:33
  • With regards to your point b), thanks for letting me know. Being new to the language, I don't really know what projects use principles that are "idiomatic" and which ones just throw shit together because it works. Is there any open source projects that you know that follow good design principles and would be good to explore and learn from? – m-renaud Jul 06 '13 at 05:08
  • @mrenaud92: you could start with [CL-PPCRE](https://github.com/edicl/cl-ppcre). I found [Ironclad](https://github.com/froydnj/ironclad) and [CL-6502](https://github.com/redline6561/cl-6502) nicely written too. – tuscland Jul 06 '13 at 07:56