On Fri, Feb 22, 2019 at 2:35 PM Jack Firth <[email protected]> wrote:

> This feels tedious and unnecessary. Looking at the underlying API of
> make-struct-type, I can't see any way to avoid this mutual recursion.
>

There may be some situations where struct definitions make essential use of
fixed points, but the situation you're describing doesn't sound like one of
them.

When you do need fixed points, since `struct` is generative, the pure Z
combinator style of fixed point tends to come up short. If we have a
expression that needs to refer to its own results, usually that's easy as
long as the results are functions: We locally bind some *other* functions
which work by first running the expression again. Running a (struct ...)
expression again produces a different result, so this isn't so viable.

Instead, Racket's mutation-based fixed points with `define` and friends
come in handy here because they only have to run the expression once. Since
`struct` has the `define` functionality built in, that seems to be the
easiest way to go. You're seeing some use-before-definition errors that
result from this, but you'd get that kind of error with the Z combinator
style too; it would just show up as an infinite loop instead.

It sounds like you'd like to avoid fixed points altogether in this case. I
think you actually can refactor your example to do that.

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.

By the way, I've noticed the documentation says `prop:custom-write` is
deprecated and to use `gen:custom-write` instead. If you do that, I think
you do have to go back to using fixed points again. The technique I
described doesn't help with generic interfaces, just structure type
properties.

-- 
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