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)



On Thu, Sep 16, 2021 at 2:12 PM Dimaugh Silvestris <
dimaughsilvest...@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)
>
> On Thu, 16 Sept 2021 at 22:35, Sorawee Porncharoenwase <
> sorawee.pw...@gmail.com> wrote:
>
>> 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)
>>
>>
>>
>>
>>
>> On Thu, Sep 16, 2021 at 1:21 PM Dimaugh Silvestris <
>> dimaughsilvest...@gmail.com> wrote:
>>
>>> (sorry if I'm asking too many questions about macros lately, I'm
>>> learning about them but I keep running into scenarios I can't find
>>> documentation for)
>>>
>>> I'm trying to capture the value of a variable whose identifier I can
>>> only get with format-id, inside a with-syntax.
>>> Something like this pseudocode (imagine name-foo contains a list of
>>> symbols):
>>> (define-syntax (my-macro stx)
>>>   (syntax-case stx ()
>>>     ((_ name other-args ...)
>>>      (with-syntax* ((varname (format-id #'name "~a-foo" #'name))
>>>                     (varval (cons (datum->syntax #'varname)
>>> (datum->syntax #'(other-args ...)))))
>>>        #'(define name (λ varval (print varval)))))))
>>>
>>>
>>> Which of course doesn't work. I understand this might have to do with
>>> how macros work at an earlier phase than runtime, so is it impossible?
>>>
>>> --
>>> 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/CAN4YmRF%3Do3NsXOvK2fvUDeYL_jfA9r946%3D%3DguoGb_%3DKyS%3Dm%2Bxw%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/racket-users/CAN4YmRF%3Do3NsXOvK2fvUDeYL_jfA9r946%3D%3DguoGb_%3DKyS%3Dm%2Bxw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>

-- 
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/CADcuegvgnUpin2sMeg%3DPHRAN4gBRDx0HpUAuz8dM3aaZ8uAGvw%40mail.gmail.com.

Reply via email to