Restrictions of :initfrom?

Discussion of Common Lisp

Restrictions of :initfrom?

Postby wvxvw » Sun Jul 10, 2011 4:09 am

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:

Code: Select all
(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.
wvxvw
 
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Restrictions of :initfrom?

Postby marcoxa » Mon Jul 11, 2011 12:53 am

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
marcoxa
 
Posts: 69
Joined: Thu Aug 14, 2008 6:31 pm

Re: Restrictions of :initfrom?

Postby wvxvw » Mon Jul 11, 2011 1:08 am

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 :)).
wvxvw
 
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests

cron