Hi all - <br><br>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?
<br><br>Below is the sample code that reproduces the issue - note I had to modify swindle/extra.ss and change <br><br>(define struct-type->class* ...) to <br>(define* struct-type->class* ...) to export the definition for the syntax to work.
<br><br>Any help is appreciated - thanks! <br><br>(require (lib "swindle.ss" "swindle")<br> (lib "extra.ss" "swindle")<br> (lib "kw.ss")<br> (planet "
struct.ss" ("ryanc" "macros.plt" 1)))<br>(require-for-syntax (planet "stx.ss" ("ryanc" "macros.plt" 1)))<br><br>(defsyntax* (defstruct* stx) ; wraps around define-struct* and struct-type->class* for custom struct
<br> (define <>-re #rx"^<(.*)>$")<br> (define (<>-id? id)<br> (and (identifier? id)<br> (regexp-match? <>-re (symbol->string (syntax-e id)))))<br> (define (<>-id id)
<br> (datum->syntax-object <br> id <br> (string->symbol<br> (regexp-replace <>-re (symbol->string (syntax-e id)) "\\1"))))<br> (syntax-case stx ()<br> ((_ (name super) (slot ... ) exp ...)
<br> (<>-id? #'name)<br> (with-syntax* ((name-sans-<> (<>-id #'name))<br> (struct:name (datum->syntax-object #'stx (string->symbol (string-append "struct:" (symbol->string (syntax-e #'name-sans-<>))))))
<br> (make-struct (datum->syntax-object #'stx (string->symbol (string-append "make-" (symbol->string (syntax-e #'name-sans-<>))))))<br> (super (if (syntax-e #'super)
<br> (<>-id #'super)<br> #'super)))<br> (quasisyntax/loc stx <br> (begin <br> (define-struct* name-sans-<> (slot ...)
<br> #,@(if (syntax-e #'super) #'((#:super super)) #'())<br> (#:inspector (make-inspector))<br> exp ...)<br> (define name <br> (let () <br> (struct-type->class* struct:name make-struct '(slot ...))))))))
<br> ((_ name (slot ...) exp ...) <br> (<>-id? #'name)<br> #'(_ (name #f) (slot ...) exp ...))<br> ((_ name more ...) <br> (not (<>-id? #'name))<br> (raise-syntax-error #f "requires a name that looks like \"<...>\"" stx #'name))))
<br><br>(defstruct* <foo1> (value)<br> (#:guard (lambda (value struct)<br> (values value))))<br>(make <foo1> 1) ; returns a regular struct <br><br>(defstruct* <foo2> (value)<br> (#:guard (lambda (value struct) ; can the value be null?
<br> (values value)))<br> (#:procedure<br> (lambda/kw (object #:optional (value (void)))<br> object))<br> )<br>(make <foo2> 1) ; returns a swindle instance?? how? <br><br><br><br><br><br><br><br>
<div><span class="gmail_quote">On 9/6/07, <b class="gmail_sendername">YC</b> <<a href="mailto:yinso.chen@gmail.com">yinso.chen@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks Will! I tried it and it works ;) except the generic make method - i.e. can't do (make <my-struct> ...). <br><br>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.
<br><br>I guess to fully integrate with the make method the function struct-type->class* should be exported...?<br><br>Thanks!<br><span class="sg">yc</span><div><span class="e" id="q_114dc56c1b3b9f86_2"><br><br><div><span class="gmail_quote">
On 9/6/07, <b class="gmail_sendername">Will M Farr
</b> <<a href="mailto:farr@mit.edu" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">farr@mit.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sep 6, 2007, at 2:43 PM, YC wrote:
<br><br>> Hi all -<br>><br>> how can one create a custom struct (i.e. make-struct-type or define-<br>> struct* from ryanc) but make it work with swindle's object system?<br><br>Keep the structure type descriptor (the first value which make-struct-
<br>type returns), and use swindle's struct-type->class on that<br>descriptor to obtain a class object which describes structs of that<br>type. For example:<br><br>(define-values (struct:my-struct make-my-struct my-struct? my-struct-
<br>get my-struct-set!)<br> (make-struct-type 'my-struct #f 1 0))<br><br>(define <my-struct> (struct-type->class struct:my-struct))<br><br>Now you can use <my-struct> anywhere a class object could be used in
<br>Swindle.<br><br>Good luck!<br><br>Will<br><br></blockquote></div><br>
</span></div></blockquote></div><br>