I've written an experimental macro `structure` that tries to make it
convenient to write custom wrappers for the constructor and the match
expander that use the name of the struct:
If you're interested in doing something like that as a one-off, the
implementation is here
Basically the approach is to use #:name and #:constructor-name to avoid
binding the name of the struct (which is still used for printing and error
reporting), define the actual constructor wrapper function you want, then
to define-syntax the name of the struct to a compile-time data structure
implementing prop:procedure, prop:struct-info, and prop:match-expander
(unless you want the default match behavior). That sounds a little more
complicated than it is: here's an example that doesn't override the default
(require (for-syntax racket/struct-info
(struct info-record (info macro)
#:property prop:procedure (struct-field-index macro)
#:property prop:struct-info (λ (this) (info-record-info this))))
(struct foo (A B C)
(define (make-foo A B)
(raw-make-foo A B (+ A B)))
(extract-struct-info (syntax-local-value #'foo-transformer))
All of that said, in practice, I haven't found myself reaching for my
`structure` macro as frequently as I expected to when I wrote it. Most of
the cases seem to fall into one of two categories. If I'm writing a trivial
script, I usually find it sufficient to follow the convention of writing a
constructor wrapper named `make-foo`. On the other hand, in a larger
project, it becomes reasonable to use the module system to manage renaming
and encapsulation, and typically I have a bunch of data-structures that I
want to handle in a consistent, project-specific way.
On Sat, Mar 10, 2018 at 1:19 AM, Milo Turner <iital...@gmail.com> wrote:
> You can also set! the extra constructor
> #lang racket
> (struct foo [a b c]
> #:extra-constructor-name -foo
> (define ((foo* make-foo) a b)
> (make-foo a b (+ a b)))
> (set! -foo (foo* -foo))
> (foo 1 2) ; -> (foo 1 2 3)
> (match (foo 1 2)
> [(foo _ _ c) c]) ; -> 3
> However I usually just provide a custom make-X function as the only
> constructor. If you're hiding initialization details through the
> constructor it seems a bit
> odd to not hide these details in the match expander. So if you really want
> full encapsulation you should write the match expander as well. But in
> that's usually overkill.
> On Friday, March 9, 2018 at 10:24:00 PM UTC-5, Jon Zeppieri wrote:
>> On Fri, Mar 9, 2018 at 9:35 PM, Kevin Forchione <lys...@gmail.com> wrote:
>>> Is it possible to initialize a struct field based on values from
>>> previously defined fields? Something equivalent to let* where
>>> >(struct foo (A B C))
>>> >(foo 1 2) would produce (foo 1 2 3) for example?
>>> As far as I know, the only way to do this is to write your own function
>> that calls the struct constructor. You can do this in a module, and only
>> export your constructor, instead of the default struct constructor.
>> If you want the constructor and the match expander to use the same
>> identifier, then you need to do a bit more work. For example:
>> #lang racket/base
>> (require racket/match
>> (for-syntax racket/base
>> (struct foo* (A B C))
>> (define (foo a b)
>> (foo* a b (+ a b)))
>> (define-match-expander $foo
>> (syntax-rules ()
>> [(foo a b c) (foo* a b c)])
>> (make-variable-like-transformer #'foo))
>> (provide (rename-out [$foo foo]
>> [foo*-A foo-A]
>> [foo*-B foo-B]
>> [foo*-C foo-C])))
>> Of course, even here, an instance will print as #<foo*>. You can
>> implement gen:custom-write to fix that.
>> There might be a better way to do this, but this is what I've used in the
>> - Jon
> 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.
> For more options, visit https://groups.google.com/d/optout.
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
For more options, visit https://groups.google.com/d/optout.