[racket-dev] cstruct and properties

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Nov 11 11:39:07 EST 2012

At Wed, 7 Nov 2012 20:00:51 +0100, Tobias Hammer wrote:
> i am currently playing around with properties attached to cstructs and ran  
> into some problems.
> 
> Normally, with racket structs, i can access a property without an instance  
> of the struct via the struct type descriptor (struct:S). There seems to be  
> nothing similar for cstructs, so the only way i found to check for or  
> access a property is to create a dummy instance solely for this purpose.
> Would it be possible to add a similar type descriptor for cstructs? To  
> account for the incompatibility with the one for structs it might make  
> sense to name it different, maybe cstruct:S or similar.

I've added it as `struct:cpointer:S' --- sticking with "struct:"
because it is a structure type in the usual sense, but adding
"cpointer:" because it corresponds to the wrapper struct, and not
really to `S' instances.

> If such a descriptor would exists it should be included in the transformer  
> bindings for cstructs. Another info i am really missing there is the  
> super-type field. As far as i can see, it would be a valid semantic to set  
> it to the identifier if the cstruct was created by type and to set it #f  
> if it got its super-type by cpointer.

After looking into this, I'm not sure what you had in mind after all.

I fixed a related issue, which is that a cstruct subtype now inherits
its parent's wrapper and properties. For example,

 (define-values (p p? p-ref) (make-struct-type-property 'my-p))
 (define-cstruct _S ([a (_array _byte 23)])
    #:property p (lambda () _S))
 (define-cstruct (_T _S) ())
 (p? (ptr-ref (malloc _T) _T))

produces #t.

But I think that's not all that you were getting at, so maybe an
example would help.

> But one more thing on properties. A source of really nasty bugs is if you  
> try to use the ctype of a cstruct inside the property:
> 
> (define-values (p p? p-ref) (make-struct-type-property 'my-p))
> (define-cstruct _S ([a (_array _byte 23)])
>    #:property p _S)

Fixed in a way that turns that into an error for a too-early use of
`_S', as we discussed.


Posted on the dev mailing list.