In general, it would be helpful to provide an example of the macro use, so that we know what you want to do. If it doesn't work, it would be helpful to provide the buggy program and an error message so that we can help with the issue that you are encountering.
From my guess, you have a variable named abc-foo
somewhere, and with this macro, you wish to define a function named abc
that can access the value of abc-foo
? If so, here’s an example of a working program:
#lang racket
(require (for-syntax racket/syntax))
(define-syntax (my-macro stx)
(syntax-case stx ()
[(_ name other-args ...)
(with-syntax ([varname (format-id #'name "~a-foo" #'name)])
#'(define name
(λ (other-args ...)
(println (list varname other-args ...)))))]))
(define abc-foo 123)
(my-macro abc x y)
(abc 5 6) ;=> '(123 5 6)
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAN4YmRF%3Do3NsXOvK2fvUDeYL_jfA9r946%3D%3DguoGb_%3DKyS%3Dm%2Bxw%40mail.gmail.com.
--
Instead of creating hola-fields
which exists at run-time, you can (define-syntax hola ...)
to a struct containing the field information at compile-time (it probably needs to contain other information too). The struct could have prop:procedure
which in this case will be the syntax transformer that generates struct creation. Looking up the field compile-time information then can be done by using syntax-local-value
or static
syntax class. See section 2.1 in https://www.cs.utah.edu/plt/publications/jfp12-draft-fcdf.pdf for explanations.
What’s unclear to me are:
1) How would you deal with the following? Should it be an error?
(card (foo . xs))
(card foo (bar . ys))
2) How would you determine the number of fields (and field names) in the following?
> (card (line . xs))
> (line 1 2 3 4 5 6 7 8 9)
Wouldn’t it make more sense to have:
> (card (line . xs))
> (line '(1 2 3 4 5 6 7 8 9))
where xs
is the only field.
3) Shouldn’t the following result in (ciao 7 3)
?
> (card (ciao a [b 3]))
> (ciao 7)
Sorry, I haven't posted the full macro because it's long and makes use of several other functions, but I'll try to summarize what it does:
Short summary: I'm trying to have a macro (mymacro oldname newname (fields ...)) that accesses oldname-foo, which contains a list of symbols, and then define a function that takes (cons oldname-foo (fields ...)) formated as identifiers as arguments. Or at least to get the length of oldname-foo and name them whatever.
Full explanation: using make-struct-type I'm building a different struct system I call cards, where structs can be defined as a function call, which will be their constructor, and they are printed as the constructor function call that would generate them. So, for instance, we can do:> (card (hola a b #:c c))> (hola 1 2 #:c 3)(hola 1 2 #:c 3)or> (card (ciao a [b 3]))> (ciao 7)(ciao 7)> (ciao 7 4)(ciao 7 4)or even> (card (line . xs))> (line 1 2 3 4 5 6 7 8 9)(line 1 2 3 4 5 6 7 8 9)
Also the names of the fields are stored in *<name-of-the-card>-fields (this is the abc-foo of the above example), so *hola-fields contains '(a b #:c c).So far this is working perfectly, but I don't have inheritance. So when I create a card that inherits from a previous card, I need to access its *<parent-card>-fields to define a new function containing both the parent and the son fields. That is, I'm trying to get this behavior:> (card (hola a #:b b))> (card hola (ciao c)) ;;; should expand to (define (ciao a #:b b c) ...), among other things> (ciao 1 #:b 2 3)
(ciao 1 #:b 2 3)
On Thu, Sep 16, 2021 at 2:12 PM Dimaugh Silvestris <dimaughs...@gmail.com> wrote:Sorry, I haven't posted the full macro because it's long and makes use of several other functions, but I'll try to summarize what it does:
Short summary: I'm trying to have a macro (mymacro oldname newname (fields ...)) that accesses oldname-foo, which contains a list of symbols, and then define a function that takes (cons oldname-foo (fields ...)) formated as identifiers as arguments. Or at least to get the length of oldname-foo and name them whatever.
Full explanation: using make-struct-type I'm building a different struct system I call cards, where structs can be defined as a function call, which will be their constructor, and they are printed as the constructor function call that would generate them. So, for instance, we can do:
> (card (hola a b #:c c))> (hola 1 2 #:c 3)(hola 1 2 #:c 3)or> (card (ciao a [b 3]))> (ciao 7)(ciao 7)> (ciao 7 4)(ciao 7 4)or even> (card (line . xs))> (line 1 2 3 4 5 6 7 8 9)(line 1 2 3 4 5 6 7 8 9)
Also the names of the fields are stored in *<name-of-the-card>-fields (this is the abc-foo of the above example), so *hola-fields contains '(a b #:c c).So far this is working perfectly, but I don't have inheritance. So when I create a card that inherits from a previous card, I need to access its *<parent-card>-fields to define a new function containing both the parent and the son fields. That is, I'm trying to get this behavior:> (card (hola a #:b b))> (card hola (ciao c)) ;;; should expand to (define (ciao a #:b b c) ...), among other things> (ciao 1 #:b 2 3)
(ciao 1 #:b 2 3)
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CADcuegvgnUpin2sMeg%3DPHRAN4gBRDx0HpUAuz8dM3aaZ8uAGvw%40mail.gmail.com.
A) What exactly are you trying to do, because I think I've got it but I'm still fuzzy.
B) Why are you trying to do it?
C) Is there a simpler / more Racket-ish way to do it?
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.
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:
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
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."
2) (card (line . xs)) has only one field, xs. Of course, you could also define it as a normal field which contains a list, but there's some other scenarios where I found it more elegant to represent it as a dotted argument (like representing s-expressions as a struct).
Oh sorry, that was a typo. I meant currently you expect
> (card (line . xs))
> (line 1 2 3 4 5 6 7 8 9)
(line 1 2 3 4 5 6 7 8 9)
to be the output, but I was asking if:
> (card (line . xs))
> (line 1 2 3 4 5 6 7 8 9)
(line '(1 2 3 4 5 6 7 8 9))
makes more sense. In any case, your response clears things up that there is indeed only one field. You simply want it to be printed like that.
This is actually a pretty fun problem. Here’s a quick prototype. Dropping it here in case anyone is interested:
#lang racket
(require syntax/parse/define
(for-syntax syntax/parse/lib/function-header
racket/syntax
racket/list
racket/struct-info))
(begin-for-syntax
(struct my-struct-info (fields args ctor)
#:property prop:procedure
(λ (inst stx)
(syntax-parse stx
[(_ args ...) #`(#,(my-struct-info-ctor inst) args ...)]
[x:id #'#,(my-struct-info-ctor inst)]))))
(define-syntax-parse-rule (define-accessors+predicate
{~var struct-id (static values #f)}
name:id)
#:with (fields ...) (struct-field-info-list (attribute struct-id.value))
#:do [(define the-struct-info (extract-struct-info (attribute struct-id.value)))]
#:with predicate (list-ref the-struct-info 2)
#:with (accessors ...) (list-ref the-struct-info 3)
#:with new-predicate (format-id #'name "~a?" #'name)
#:with (new-accessors ...)
(map (λ (id) (format-id #'name "~a-~a" #'name id)) (attribute fields))
(begin
(define new-predicate predicate)
(define new-accessors accessors) ...))
(define-syntax-parse-rule
(card
{~optional (~var super-id (static my-struct-info? "card type"))}
{~and header:function-header (_:id . args)})
#:with ((all-fields ...) all-args)
(let ([info (attribute super-id.value)])
(cond
[info
(unless (list? (syntax-e (my-struct-info-args info)))
(raise-syntax-error 'card
"supertype can't have variadic fields"
this-syntax))
#`(({~@ . #,(my-struct-info-fields info)} . header.params)
({~@ . #,(my-struct-info-args info)} . args))]
[else #'(header.params args)]))
#:fail-when (check-duplicates (attribute all-fields) #:key syntax-e)
"duplicate field name"
(begin
(struct shadow (all-fields ...)
#:transparent
;; TODO: implement gen:custom-write (probably with make-constructor-style-printer)
;; to customize struct value printing
#:reflection-name 'header.name)
(define-accessors+predicate shadow header.name)
(define (shadow-ctor . all-args)
(shadow all-fields ...))
(define-syntax header.name
(my-struct-info #'(all-fields ...)
#'all-args
#'shadow-ctor))))
(let ()
(card (hola a b #:c c))
(println (hola 1 2 #:c 3))
(card (ciao a [b 3]))
(println (ciao 7))
(println (ciao 7 4))
(card (line . xs))
(println (line 1 2 3 4 5 6 7 8 9)))
(let ()
(card (hola a #:b b))
(card hola (ciao c))
(define v (ciao 1 #:b 2 3))
(println v)
(println (list (ciao-a v) (ciao-b v) (ciao-c v)))
(println (list (ciao? v) (hola? v))))
(let ()
(card (foo . xs))
;; uncomment should result in a syntax error
(card #;foo (bar . ys))
(card (a xs))
;; uncomment should result in a syntax error
(card #;a (b xs))
(void))
What I did not implement is making the struct value printed in the way you want, but that can be adjusted by using gen:custom-write
. Note that I didn’t (re)use struct
‘s supertype feature since you want fields in the opposite order.