Dynamic reload?

343 views
Skip to first unread message

Chris P

unread,
Sep 1, 2019, 12:24:07 PM9/1/19
to chez-scheme
Hi folks,

I'm new to Scheme and exploring Chez and Racket, both of which
have been awesome so far!

I'm trying to see if there is a way to dynamically reload code
from a library in Chez scheme (AKA hot reload). The parallel in
Racket would be the use of `dynamic-require` and
`dynamic-rerequire`.

I seem to be able to do this in the REPL:

> (load "reload-lib.ss")
> (import (reload-lib))
> (foo)
Foo!


Then if I change `foo` in `reload-lib.ss` to print out `Bar!`
instead I get the correct input in the same REPL:

> (load "reload-lib.ss")
> (import (reload-lib))
Bar!


I cannot seem to get the same to work from within a library. For
example, calling `bar` from the following library does not
actually reload `reload-lib.ss`, it just loops printing `Foo!`:

(library (reload-main)
 
(export bar)
 
(import (chezscheme))

 
(define (bar)
   
(load "reload-lib.ss")
   
(let ()
     
(import (reload-lib))
     
(foo)
     
(sleep (make-time 'time-duration 0 1))
      (bar)))
)


Is there a way to do what I'm trying to do? If I'm way off track,
a nudge in the right direction would be greatly appreciated!

A bit of an aside: I had to put everything in an empty `let` to
get `import` to work. Otherwise I got this:

Exception: invalid context for definition (import (reload-lib)) at line 7, char 5 of reload-main.ss


The Chez scheme docs indicate that `import` can be used in a
local body, which I assumed, perhaps incorrectly, is everything
after the variable and formals in the define form?

Thanks,
Chris

Travis Hinkelman

unread,
Sep 6, 2019, 7:26:49 AM9/6/19
to chez-scheme
Hi Chris,

I haven't been following this mailing list very long, but it doesn't seem to be very active. You might have more luck getting an answer at r/scheme on reddit or #scheme on freenode.

Best,

Travis

Graham Watt

unread,
Sep 7, 2019, 4:56:12 PM9/7/19
to chez-scheme
Chez Scheme will evaluate library code at most once. Subsequent imports will not re-evaluate the library. Whille you can normally re-import new library code following a load in the REPL, inside of R6RS programs and libraries imports and loads are entirely disconnected. The library is resolved once by import at compile-time before load ever executes. The location of the import is unnecessary - all imports are processed once. You can see this behavior with the parameter import-notify:
> (import-notify #t)

> (library (reload-main)
   
(export bar)
   
(import (chezscheme))
 
   
(define (bar)
     
(load "reload-lib.ss")
     
(let ()
       
(import (reload-lib))
       
(foo)
       
(sleep (make-time
'time-duration 0 1))
        (bar)))
    )
import: did not find source file "reload-lib.chezscheme.sls"
import: found source file "reload-lib.ss"
import: did not find corresponding object file "reload-lib.so"
import: loading source file "reload-lib.ss"
> (top-level-program
    (import (reload-main))
    (bar))
foo
foo
...

If you want hot-code reloading, your best bet is to avoid R6RS libraries* / programs. You can get pretty far just using load in normal files and scripts (running with --script).
---

* As you found out, you can reload libraries by combining load and import if you're running in the repl. However, there's no builtin mechanism to reload a hierarchy of libraries. You would need to combine (library-requirements ...) to get a list of a library's dependencies, and library-directories and library-extensions to find the source file to reload.

Chris Pettitt

unread,
Sep 7, 2019, 7:07:43 PM9/7/19
to Graham Watt, chez-scheme
Thanks Graham! This level of detail helps a lot and import-notify looks like it going to be a helpful option.

Best,
Chris



--
You received this message because you are subscribed to the Google Groups "chez-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chez-scheme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chez-scheme/3ba60c9e-87ba-44d6-858f-fcb09ab768a9%40googlegroups.com.

Chris Pettitt

unread,
Sep 8, 2019, 10:46:39 PM9/8/19
to Graham Watt, chez-scheme
Just a quick follow up to say that this was a huge help. I now have hot reloading working for a small game engine I'm working on and I can nearly instantaneously change the geometry shipped off to the GPU, among other things. The hot reload actually works faster than the equivalents I've done in C++! A very exciting start with Chez :).

Best,
Chris

Robert Calco

unread,
Apr 24, 2020, 6:53:38 AM4/24/20
to chez-scheme
I'd like to learn more details about what you're doing to get hot reloading to work. Any chance you (or anyone else) could post a gist at github or a simple working example as a repo?
To unsubscribe from this group and stop receiving emails from it, send an email to chez-...@googlegroups.com.

Jerry Kotland

unread,
Sep 26, 2020, 1:37:47 AM9/26/20
to chez-scheme
I've had success using reimport for reloading R6RS libs from https://github.com/akce/chez-dynamic. At least with version 6.5.2 Chez scheme.
Reply all
Reply to author
Forward
0 new messages