This library provides parameter objects, which are dynamically scoped variables. Parameters allow programs to temporarily override values in a specific dynamic context without affecting other concurrent execution contexts.
Base class for parameter objects.
Returns #t if obj is a parameter object, otherwise #f.
Creates a new parameter object with initial value init.
If converter is provided, it must be a procedure that accepts one argument. The converter is applied to init and any value assigned to the parameter later.
(import (sagittarius parameters))
(define current-level (make-parameter 0))
(current-level) ; => 0
(current-level 5)
(current-level) ; => 5
;; With converter
(define current-port-name
(make-parameter "default"
(lambda (v) (if (string? v) v (symbol->string v)))))
(current-port-name 'stdout) ; Converts symbol to string
(current-port-name) ; => "stdout"
Thread parameters are parameters whose values are stored in thread-local storage.
Each thread maintains its own value for a thread parameter.
Subclass of <parameter> for thread-local parameters.
Returns #t if obj is a thread parameter object, otherwise #f.
Creates a new thread parameter with initial value init.
If converter is provided, it is applied to values as with make-parameter.
(import (sagittarius parameters))
;; Thread parameters store values in thread-local storage
(define thread-id (make-thread-parameter 0))
;; Each thread would see its own value
(thread-id 42)
(thread-id) ; => 42
;; Thread parameters can also have converters
(define thread-name
(make-thread-parameter "main"
(lambda (v) (if (string? v) v (symbol->string v)))))
(thread-name 'worker)
(thread-name) ; => "worker"
Parameterization objects capture a snapshot of parameter bindings.
Returns the current parameterization object.
Returns #t if obj is a parameterization object, otherwise #f.
Calls thunk with the given parameterization installed as the current parameterization. Returns the value(s) returned by thunk.
(define p (make-parameter 1))
(define saved (current-parameterization))
(parameterize ((p 2))
(set! saved (current-parameterization)))
(p) ; => 1
(call-with-parameterization saved (lambda () (p))) ; => 2
Low-level thread-local storage API.
Class for thread-local storage objects.
Returns #t if obj is a thread-local storage object, otherwise #f.
Creates a thread-local storage object with initial value.
If inheritable? is true, the value is inherited by child threads.
Returns the current thread's value for thread-local.
Sets the current thread's value for thread-local to obj.
Evaluates body with each _parameter_s temporarily bound to its corresponding _value_s. The previous values are restored when control leaves body either normally or by a continuation jump.
This form uses continuation marks and integrates properly with delimited continuations.
(define p (make-parameter 1))
(p) ; => 1
(parameterize ((p 2))
(p)) ; => 2
(p) ; => 1
;; Multiple parameters
(define p1 (make-parameter 10))
(define p2 (make-parameter 20))
(parameterize ((p1 100) (p2 200))
(list (p1) (p2))) ; => (100 200)
[SRFI-39] Similar to parameterize, but uses dynamic-wind for
parameter restoration to adhear to SRFI-39 semantics.
[SRFI-226] Temporarily swaps the values of _parameter_s the results of _expr_s evaluating body.
Similar to parameterize, but uses direct value swapping. The
original values are restored using dynamic-wind.
(define p1 (make-parameter 1))
(define p2 (make-parameter 2))
(temporarily ((p1 10) (p2 20))
(list (p1) (p2))) ; => (10 20)
(list (p1) (p2)) ; => (1 2)