This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Restrictions of :initfrom?

3 posts · 1006 views

Or, rather, how could I use a value that needs to be computed rather than a constant? All examples I find are too simplistic in that they show how to use :initfrom when initializing slot of a class with a literal value such as number or string. However I need to use an object. Something along these lines:
(defstruct
    (as3-fqname 
      (:print-function
       (lambda (struct stream depth)
	   (declare (ignore depth))
	   (format stream "~a:~a" 
		   (as3-fqname-package struct)
		   (as3-fqname-name struct)))))
  (package "" :type string)
  (name "" :type string))

;; (defstruct (amf-object
;; 	     (:print-function
;; 	      (lambda (struct stream depth)
;; 		  (declare (ignore depth))
;; 		  (format stream "[~a object]" 
;; 			  (amf-object-class struct)))))
;;   (class (make-as3-fqname :as3-name "Object")
;; 	     :type as3-fqname)
;;   (properties (make-hash-table :test 'equal)))

;; (make-amf-object :class (make-as3-fqname :package "foo" :name "Bar"))

(defclass amf-object ()
  ((constructor 
    :initarg :constructor ;; if I uncomment the below I'll get error
    ;; :initfrom (make-as3-fqname :as3-name "Object")
    :type as3-fqname)
   (properties
    ;; :initfrom (make-hash-table :test 'equal)
    :type hash-table)))
Since it doesn't compile it sends me to this page: http://www.lispworks.com/documentation/ ... /07_ab.htm for clarification, and I can't understand what is the error I'm making. The commented struct above shows what I would like to use hypothetically, but, I'd need it to be a class.

EDIT: Sorry, never mind it, silly typo :) It should've been :initform instead.

Re: Restrictions of :initfrom?

If I understand correctly your needs you should use INITIALIZE-INSTANCE on objects, while for structures you have a few options; e.g., using &aux parameters in a user supplied :constructor. Shameless plug here.

Cheers
Marco Antoniotti

Re: Restrictions of :initfrom?

Thanks, but it was basically a typo :) I.e. I swapped "o" and "r" in "initform" (I thought it meant initialize this property from the expression given afterwards, maybe for English speakers it would sound strange, but for foreigners it still makes sense :)).