unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Christopher Lam <christopher.lck@gmail.com>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: srfi-9 vs make-record-type
Date: Mon, 05 Aug 2019 14:18:06 -0400	[thread overview]
Message-ID: <87lfw78ohy.fsf@netris.org> (raw)
In-Reply-To: <CAKVAZZKUKEJZtQiEZemDSN17S1_D0U_pe_=DdeiL4C9Ve8vpSg@mail.gmail.com> (Christopher Lam's message of "Sun, 21 Jul 2019 10:20:29 +0000")

Hi Christopher,

Christopher Lam <christopher.lck@gmail.com> writes:

> Hi All
> In experiments converting legacy code to use srfi-9 records, I'm finding
> the latter doesn't travel well across modules.
>
> See simple code below -- m1.scm runs fine however m2.scm borks when
> creating srfi-9 record object
>
> Any clue why srfi-9 can't be exported?
>
> For various reasons I must keep (load "module.scm") mechanism
> ----m1.scm follows----
> (use-modules (srfi srfi-9))
>
> (define-record-type <person>
>   (make-person name age)
>   person?
>   (name person-name set-person-name!)
>   (age person-age set-person-age!))
>
> (define <pet> (make-record-type "pet" '(name age)))
> (define make-pet (record-constructor <pet>))
> (define pet? (record-predicate <pet>))
> (define pet-name (record-accessor <pet> 'name))
> (define pet-age (record-accessor <pet> 'age))
> (define set-pet-name! (record-modifier <pet> 'name))
> (define set-pet-age! (record-modifier <pet> 'age))
> (export make-person)
> (export make-pet)
>
> (display "pet ")
> (let ((pet2 (make-pet "milou" 7)))
>   (display (pet-name pet2)))
> (display ", person ")
> (let ((person2 (make-person "james" 54)))
>   (display (person-name person2)))
> (newline)
> ----m2.scm follows----
> (load "m1.scm")
> (display "in m2:")
> (newline)
> (display "pet ")
> (let ((pet2 (make-pet "milou" 7)))
>   (display (pet-name pet2)))
> (display ", person ")
> (let ((person2 (make-person "james" 54)))
>   (display (person-name person2)))
> (newline)
> --------------------------------
> $guile m1.scm runs successfully
>
> pet milou, person james
>
> $guile m2.scm first runs m1.scm but fails to recognise the srfi-9
> make-person exists:
> pet milou, person james
> in m2:
> pet milou, person Backtrace:
>            6 (apply-smob/1 #<catch-closure 5599248723e0>)
> In ice-9/boot-9.scm:
>     705:2  5 (call-with-prompt _ _ #<procedure default-prompt-handle…>)
> In ice-9/eval.scm:
>     619:8  4 (_ #(#(#<directory (guile-user) 559924904140>)))
> In ice-9/boot-9.scm:
>    2312:4  3 (save-module-excursion _)
>   3831:12  2 (_)
> In /home/chris/sources/caca/m2.scm:
>      8:15  1 (_)
> In unknown file:
>            0 (_ "james" 54)
>
> ERROR: Wrong type to apply: #<syntax-transformer make-person>

The problem here is that 'make-person' is a macro.  Macros are expanded
at compile time, and must therefore be available at compile time.

When you compile 'm2.scm', those macros are not available, because
'load' only has effects at run-time, by design.  Since there is no macro
definition for 'make-person' at compile-time, Guile's compiler generates
code that assumes it will be a procedure.

In summary, you cannot use 'load' to import macros from another module.

> For various reasons I must keep (load "module.scm") mechanism

If you can share those reasons, perhaps I can help you find another
solution.

      Regards,
        Mark



  parent reply	other threads:[~2019-08-05 18:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-21 10:20 srfi-9 vs make-record-type Christopher Lam
2019-07-21 19:21 ` John Cowan
2019-08-05 18:18 ` Mark H Weaver [this message]
2019-08-26 12:20   ` Christopher Lam
2020-07-13 13:07     ` Christopher Lam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lfw78ohy.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=christopher.lck@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).