Note that there are some explanatory texts on larger screens.

plurals
  1. POIdiomatic Way Of Creating Class Instances From Config Files
    text
    copied!<p>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</p> <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.</p> <p>So, for a simple example, if I have the following class:</p> <pre class="lang-lisp prettyprint-override"><code>(defclass my-class () ((name :initarg :name :accessor name) (x :initarg :x :initform 10 :accessor x) (y :initarg :y :initform nil :accessor y))) </code></pre> <p>and</p> <pre class="lang-lisp prettyprint-override"><code>(defmethod initialize-instance :after ((obj my-class) &amp;key) (with-slots (x y) obj (setf y (* 2 x)))) </code></pre> <p>I would like a way of specifying in an external file, say <code>instance-a.lisp</code></p> <pre class="lang-lisp prettyprint-override"><code>(possibly-some-macro "A" :x 5) </code></pre> <p>But when constructing an instance the value x must be normalized first some how. The eventual call to <code>make-instance</code> would look something like this:</p> <pre class="lang-lisp prettyprint-override"><code>(make-instance 'my-class :name (name-value-from-config) :x (normalize (x-value-from-config)) </code></pre> <p>Where <code>(name-value-from-config)</code> 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.</p> <p>My initial thought would be to turn whatever is in the config file into a hash table or a plist with the appropriate keys.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload