[plt-scheme] How get name of structure

From: Daniel Yoo (dyoo at cs.wpi.edu)
Date: Wed Apr 11 06:25:27 EDT 2007

> Or better allow f to work for structs controlled by the current 
> inspector too:
>
> (define-struct structure1 (f1) (make-inspector))
> (define-struct structure2 (f1 f2))
> (define x (make-structure1 1))
> (define y (make-structure2 'a 'b))

If we do make the structures inspectible, then STRUCT-TYPE-INFO may 
be what we're looking for: it gives us the name-symbol of the structure 
type.

http://download.plt-scheme.org/doc/360/html/mzscheme/mzscheme-Z-H-4.html#node_idx_808


So something like this should also do the trick:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module get-struct-type-name mzscheme
   (provide get-struct-type-name)

   ;; get-struct-type-name: struct -> symbol
   ;; Tries to get the name of the type of a-structure.
   ;; Raises an error if it can't inspect.
   (define (get-struct-type-name a-structure)
     (let-values ([(struct-type skipped?) (struct-info a-structure)])
       (cond
         [struct-type
          (let-values ([(name-symbol
                         init-field-k
                         auto-field-k
                         accessor-proc
                         mutator-proc
                         immutable-k-list
                         super-struct-type
                         skipped?)
                        (struct-type-info struct-type)])
            name-symbol)]
         [else
          (error 'get-struct-type-name
                 "~s not inspectable"
                a-structure)]))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define-struct foo ())
> (get-struct-type-name (make-foo))
get-struct-type-name: #<struct:foo> not inspectable
> (define-struct bar () #f)
> (get-struct-type-name (make-bar))
bar
> 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Best of wishes!


Posted on the users mailing list.