procedurized struct returned as instance? ==> Re: [plt-scheme] swindle and custom struct

From: YC (yinso.chen at gmail.com)
Date: Thu Sep 6 19:17:02 EDT 2007

Hi all -

I'm hacking away on enabling custom struct via Swindle object system and run
into a peculiar issue - if I make a custom struct with proc-spec turned on,
instead of returning a struct, "make" returns an swindleobj.  I suspect this
is due to some inheritance hierarchy precedence but can't figure out how to
trace swindle's initialization process - any thoughts?

Below is the sample code that reproduces the issue - note I had to modify
swindle/extra.ss and change

(define struct-type->class* ...) to
(define* struct-type->class* ...) to export the definition for the syntax to
work.

Any help is appreciated - thanks!

(require (lib "swindle.ss" "swindle")
         (lib "extra.ss" "swindle")
         (lib "kw.ss")
         (planet "struct.ss" ("ryanc" "macros.plt" 1)))
(require-for-syntax (planet "stx.ss" ("ryanc" "macros.plt" 1)))

(defsyntax* (defstruct* stx) ; wraps around define-struct* and
struct-type->class* for custom struct
  (define <>-re #rx"^<(.*)>$")
  (define (<>-id? id)
    (and (identifier? id)
         (regexp-match? <>-re (symbol->string (syntax-e id)))))
  (define (<>-id id)
    (datum->syntax-object
     id
     (string->symbol
      (regexp-replace <>-re (symbol->string (syntax-e id)) "\\1"))))
  (syntax-case stx ()
    ((_ (name super) (slot ... ) exp ...)
     (<>-id? #'name)
     (with-syntax* ((name-sans-<> (<>-id #'name))
                    (struct:name (datum->syntax-object #'stx (string->symbol
(string-append "struct:" (symbol->string (syntax-e #'name-sans-<>))))))
                    (make-struct (datum->syntax-object #'stx (string->symbol
(string-append "make-" (symbol->string (syntax-e #'name-sans-<>))))))
                    (super (if (syntax-e #'super)
                               (<>-id #'super)
                               #'super)))
       (quasisyntax/loc stx
         (begin
           (define-struct* name-sans-<> (slot ...)
             #,@(if (syntax-e #'super) #'((#:super super)) #'())
             (#:inspector (make-inspector))
             exp ...)
           (define name
             (let ()
               (struct-type->class* struct:name make-struct '(slot
...))))))))
    ((_ name (slot ...) exp ...)
     (<>-id? #'name)
     #'(_ (name #f) (slot ...) exp ...))
    ((_ name more ...)
     (not (<>-id? #'name))
     (raise-syntax-error #f "requires a name that looks like \"<...>\"" stx
#'name))))

(defstruct* <foo1> (value)
  (#:guard (lambda (value struct)
             (values value))))
(make <foo1> 1) ; returns a regular struct

(defstruct* <foo2> (value)
  (#:guard (lambda (value struct) ; can the value be null?
             (values value)))
  (#:procedure
   (lambda/kw (object #:optional (value (void)))
     object))
  )
(make <foo2> 1) ; returns a swindle instance?? how?







On 9/6/07, YC <yinso.chen at gmail.com> wrote:
>
> Thanks Will!  I tried it and it works ;) except the generic make method -
> i.e. can't do (make <my-struct> ...).
>
> After looking at the defstruct code - it appears that it calls a private
> function called struct-type->class* and this function builds on top of
> struct-type->class and use it to track the slots for all the types created
> via defstruct and thus allow the structs to be created via made.
>
> I guess to fully integrate with the make method the function
> struct-type->class* should be exported...?
>
> Thanks!
> yc
>
> On 9/6/07, Will M Farr <farr at mit.edu> wrote:
> >
> > On Sep 6, 2007, at 2:43 PM, YC wrote:
> >
> > > Hi all -
> > >
> > > how can one create a custom struct (i.e. make-struct-type or define-
> > > struct* from ryanc) but make it work with swindle's object system?
> >
> > Keep the structure type descriptor (the first value which make-struct-
> > type returns), and use swindle's struct-type->class on that
> > descriptor to obtain a class object which describes structs of that
> > type.  For example:
> >
> > (define-values (struct:my-struct make-my-struct my-struct? my-struct-
> > get my-struct-set!)
> >    (make-struct-type 'my-struct #f 1 0))
> >
> > (define <my-struct> (struct-type->class struct:my-struct))
> >
> > Now you can use <my-struct> anywhere a class object could be used in
> > Swindle.
> >
> > Good luck!
> >
> > Will
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070906/5e3d37a9/attachment.html>

Posted on the users mailing list.