Email or username:

Password:

Forgot your password?
Grigory Shepelev

Question for #scheme ☯️ kings & queens 👑 ! Are there an alternative to clojure's alter-var-root clojuredocs.org/clojure.core/w in scheme?

Imagine I have a module `(X)` with `(define a 1) (define-public (f x) (+ x a))` and module `(Y)` that has `#:use-module (X)`. In `(Y)` want to alter a from `(X)` in a way that would also affect `f`. Example: (being in module `(Y)`): `(alter! a 2) (equal? (f 3) 5) => #t`

CC @wingo @rml @cwebber

14 comments
Andy Wingo

@shegeley @rml @cwebber most schemers are republicans, in the anti-monarchist sense :) but if the intended use case is global monkeypatching, then in guile `set!` is your tool. if `a` is exported, then just `set!` on `a`. if not, `(set! (@@ (X) a) new-val)`

Andy Wingo

@shegeley @rml @cwebber depends :) add #:declarative? #f to the define-module of X and see

Grigory Shepelev

@monkey1 @wingo @rml @cwebber

I'm not sure about this formulation. I want to update `x` from `(X)` "globally": in a way that everything from `(X)` that'd have `x` referenced would also update it to the new value.

Grigory Shepelev

@wingo @rml @cwebber
@monkey1

Imagine that you have a module already written by someone that does the job you want to, but there is only one variable (example `(define pi 3.14)` ) that you want just override but otherwise would like everything to work as previously. Wouldn't like to copy-paste a one of code

Grigory Shepelev

@rml @wingo @cwebber @monkey1

it won't update setted variable's value in all procedures of `(X)` that uses `x`. `(define (square r pi) (* r r pi))` <-- here pi will stay 3.14 when `square` will be called from `(Y)`

Grigory Shepelev

@rml @wingo @cwebber @monkey1 probably the answer given was toatally correct. set! and stuff will work in repl, but won't when launched in the testing env that I just set up.

blake shaw 🇵🇸

@shegeley @wingo @cwebber @monkey1 so you just need to create an alias? In that case (define my-alias the-original) works fine except when dealing with syntax. In that case, you can do

(define-syntax my-alias
(identifier-syntax the-original))

Might have left something out of that bcs im writing from my phone, currently on the move, so lmk if that didn't work and ill take a look.

Janneke

@shegeley @wingo @rml @cwebber @monkey1
Yeah, use guile-2.2 and module-define!, or create a new module and import everything else, e.g.., do something like

(define-module (math square+)
#:export (square))

(define pi 3.1415)
(define square (@@ (math square) square))
...

Go Up