Oddbean new post about | logout
 Question for #scheme ☯️  kings & queens 👑 ! Are there an alternative to clojure's alter-var-root https://clojuredocs.org/clojure.core/with-redefs 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 @c39d2605 @4c603b88 @462ebf4c 
 @c65b183b @4c603b88 @462ebf4c 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)` 
 @c39d2605 @4c603b88 @462ebf4c 
@338f82ef

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 
 @c65b183b @c39d2605 @4c603b88 @462ebf4c Do you have access to change the implementation of (X)? If so, could you define a as a parameter object? In that case, you could at least change the value of a for all of its references in the dynamic scope of (Y).