Thanks, I'll take a look into the code struct++! I'll probably get lost but
I'm sure I'll learn something.
Answering your questions:

A)  What exactly are you trying to do, because I think I've got it but I'm
>>> still fuzzy.
>>>
>>
Structs that allow constructors like any other function, and which are
printed back as the function which creates them. So, a bit like prefab
structs but with default values, keywords and (f . xs) args.

B)  Why are you trying to do it?
>>>
>>
This one is long, but I think they're cool. Also convenient in several
scenarios, like representing a DSP graph where things like keyword
arguments often come in handy. I've been replicating this behavior by hand,
it was time to write a macro.


> C) Is there a simpler / more Racket-ish way to do it?
>>>
>>
Probably, but I'm very picky and I thought Racket was good for this kind of
thing.

I notice that you're using make-struct-type instead of struct -- is that
>>> intentional or is there some specific feature you want?  I suspect I'm
>>> about to get a more experienced person telling me that I've missed
>>> something, but to the best of my knowledge struct is the more modern
>>> version and can do everything that make-struct-type can do but cleaner.
>>>
>>>
Struct is a macro built on top of make-struct-type, with higher level
features. However, I'm trying to get different features so I have to
descend into the lower level.


> As to the printing as a constructor call:  putting the #:prefab option on
>>> a struct will allow you to print it in a reversible form that can be called
>>> in order to generate the struct again, but it makes explicit all the values
>>> that go in instead of hiding them away as defaults or etc.  For example:
>>>
>>
Yes,  prefabs come painfully close to what I'm trying to do, but they still
don't allow you to define the constructor however you like (for instance,
using keyword arguments). So instead of doing (card (hola a b #:c [c 3])),
I end up writing things like:

(struct CARD (a b c)
            .....)           ;;; give it a custom-write-printer so that it
prints as 'card' and c is preceeded by #:c, which is long
(define (card a b #:c [c 3]) ...)
(define (card-a x) (CARD-a x))
...

And then I get fed up and I think, well this is what macros were made for,
right?

How are you going to handle the situation where a parent and child struct
>>> have a field with the same name?  This is entirely legit:
>>>
>>> #lang racket
>>>
>>> (struct person (name age) #:prefab)     ; age is years since birth
>>>
>>> (struct employee person (age) #:prefab) ; age is years since hiring
>>>
>>>
>>> (define bob (employee 'bob 17 3))
>>> bob
>>> (employee-age bob)
>>> (person-age bob)
>>>
>>> Output:
>>>
>>> '#s((employee person 2) bob 17 3)
>>> 3
>>> 17
>>>
>>
It wouldn't be allowed, and I consider it a bad naming choice. But at the
lower level make-struct-type, fields have no names, there's just a number
of them. Make-struct-type returns, among other things, a struct accessor
that works like vector ref: (mystruct-accessor mystruct n) to get the nth
field. Struct is just a macro on top of make-struct-type that creates a
custom accessor for each field.


>>> Going back to the earlier question:  What is it you are ultimately
>>> trying to accomplish at a high level?  i.e. Not "generate a lot of struct
>>> types and field data" but something like "store information about a
>>> hierarchical structure of <thing> in a persistent way so that it is
>>> recoverable across server restarts."
>>>
>>
Too often I find myself replicating this behavior, in a few things I'm
working on: one is a SuperCollider client, another is a drawing library
based on racket/draw, but representing things very differently, and also
some stuff with html where I didn't want to work with quasiquote and
unquote everywhere.

-- 
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 racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAN4YmRG_dD0RfxKATh6U%3DyVT5UX%3De5GnuVbfkGDfJDRdqV44pg%40mail.gmail.com.

Reply via email to