Following up on this for anyone interested; I made a small wrapper library (
rebellion/struct-descriptor 
<https://docs.racket-lang.org/rebellion/Struct_Descriptors.html>) around 
make-struct-type. You may find it interesting if you've created lots of 
struct types that all had to implement the same methods and properties. The 
library does the following:

   - Defines a struct-descriptor? 
   
<https://docs.racket-lang.org/rebellion/Struct_Descriptors.html?q=rebellion#%28def._%28%28lib._rebellion%2Fstruct-descriptor..rkt%29._struct-descriptor~3f%29%29>
 
   data type that combines the values that would be returned by 
   make-struct-type or struct-type-info.
   - Specifies that descriptors may be *initialized* or *uninitialized*, 
   where only initialized descriptors contain a reference to the created 
   struct-type? object.
   - Provides a make-struct-type/descriptor 
   
<https://docs.racket-lang.org/rebellion/Struct_Descriptors.html?q=rebellion#%28def._%28%28lib._rebellion%2Fstruct-descriptor..rkt%29._make-struct-type%2Fdescriptor%29%29>
 
   function that's like make-struct-type, but instead of accepting a list of 
   property implementations it accepts a *property maker function*. This 
   function takes the uninitialized descriptor as an argument and returns a 
   list of property implementations. The make-struct-type/descriptor function 
   returns an initialized descriptor. This makes it easier to abstract over 
   property implementations, as you can make reusable functions that accept 
   uninitialized descriptors as arguments and return property implementations. 
   Oh, and it also has keyword arguments with reasonable defaults so you don't 
   have to look up the docs for all eleven parameters to make-struct-type and 
   write the defaults for the first eight just because the list of immutable 
   positions comes ninth.

This is kind of like the make-struct-type-property API that rocketnia 
mentions:

Instead of defining your `make-list-type-writer` abstraction as a function, 
> you can define it as a structure type property. (I think 
> `prop:auto-custom-write` may be an example of this technique.).This 
> property would make use of multiple features of 
> `make-struct-type-property`: It would first obtain a list of reflective 
> information about the struct type (since that's one of the parameters 
> passed to its guard procedure), and then it would use one of its supers 
> entries to populate `prop:custom-write` based on that information. One part 
> of the reflective information is an accessor procedure, so the generated 
> `prop:custom-write` procedure can call that instead of having to refer to 
> `point-x` and `point-y`.
>
> I find it a little surprising how well this technique corresponds to the 
> needs of a program that's avoiding fixed points. The bundle of reflective 
> information only contains certain information `struct` defines, namely the 
> accessor and mutator procedures. If it contained the structure type 
> descriptor or the constructor, then it would be possible to attempt to 
> access a property value *before* it was done being computed, essentially 
> letting us run into the very kind of use-before-definition error that we'd 
> get if we were using fixed points.
>

I wanted to avoid defining struct types like prop:auto-custom-write (which 
I didn't know about, thanks rocketnia!) that are only used to derive 
implementations of other struct types. That seems to me like it needlessly 
pollutes the API surface of the data types I create, and one function that 
creates all the properties is easier for me to understand than the 
graph-like approach of properties derived from other properties. But it is 
reassuring to know that make-struct-type-property passes information about 
the struct to the guard in a way that's roughly the same as my initialized 
/ uninitialized struct descriptors.

Since then, I've used my struct descriptors to implement two things:

   - make-struct-equal+hash-property 
   
<https://docs.racket-lang.org/rebellion/Struct_Equality_Implementations.html>, 
   which implements prop:equal+hash for opaque structs in the same way as the 
   default implementation for #:transparent structs.
   - make-constructor-style-struct-write-property 
   <https://docs.racket-lang.org/rebellion/Struct_Writer_Implementations.html>, 
   which implements prop:custom-write using make-constructor-style-printer 
   
<https://docs.racket-lang.org/reference/structutils.html#%28def._%28%28lib._racket%2Fstruct..rkt%29._make-constructor-style-printer%29%29>

Unfortunately I can't implement generic interfaces because there is no 
non-macro interface to that. See 
https://github.com/racket/racket/issues/1647 for some more background on 
that.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to