Liking cljdoc? Tell your friends :D

co.multiply.scoped


askclj/smacro

(ask sym)
(ask sym default)

Access a scoped value by symbol.

Returns the value bound via scoping if present, otherwise falls back to the var's root binding. If no default is given, throws when the var is unbound and not in scope.

(def ^:dynamic *user-id* :default)

(scoping [*user-id* 123]
  (ask *user-id*))  ;=> 123

(ask *user-id*)     ;=> :default (falls back to var value)

The two-arity form evaluates and returns default only if the var is unbound and not in scope:

(def ^:dynamic *user*)

(ask *user* :anonymous)  ;=> :anonymous (var is unbound)

Note: In CLJS, a var with value nil is indistinguishable from an unbound var. When not in scope, (ask *var* default) returns default if the var's value is nil. However, explicitly scoping to nil works correctly: (scoping [*var* nil] (ask *var* default)) returns nil.

Access a scoped value by symbol.

Returns the value bound via `scoping` if present, otherwise falls back
to the var's root binding. If no default is given, throws when the var
is unbound and not in scope.

```clojure
(def ^:dynamic *user-id* :default)

(scoping [*user-id* 123]
  (ask *user-id*))  ;=> 123

(ask *user-id*)     ;=> :default (falls back to var value)
```

The two-arity form evaluates and returns `default` only if the var is
unbound and not in scope:

```clojure
(def ^:dynamic *user*)

(ask *user* :anonymous)  ;=> :anonymous (var is unbound)
```

Note: In CLJS, a var with value `nil` is indistinguishable from an unbound
var. When not in scope, `(ask *var* default)` returns `default` if the var's
value is `nil`. However, explicitly scoping to `nil` works correctly:
`(scoping [*var* nil] (ask *var* default))` returns `nil`.
sourceraw docstring

assoc-scopeclj/smacro

(assoc-scope scope & bindings)

Extend an existing scope map with additional bindings.

Takes a scope (as returned by current-scope) and var-value pairs, returns a new scope with the bindings added. Does not establish the scope - use with-scope for that. A value of skip leaves the existing binding, or its absence, unchanged; nil and false are ordinary values.

(with-scope (assoc-scope captured-scope *user-id* 123)
  (ask *user-id*))  ;=> 123
Extend an existing scope map with additional bindings.

Takes a scope (as returned by `current-scope`) and var-value pairs,
returns a new scope with the bindings added. Does not establish the
scope - use `with-scope` for that. A value of `skip` leaves the existing
binding, or its absence, unchanged; `nil` and `false` are ordinary values.

```clojure
(with-scope (assoc-scope captured-scope *user-id* 123)
  (ask *user-id*))  ;=> 123
```
sourceraw docstring

current-scopeclj/smacro

(current-scope)

Returns the current scope map, or an empty map if no scope is active.

The scope map contains var->value bindings set via scoping.

Returns the current scope map, or an empty map if no scope is active.

The scope map contains var->value bindings set via `scoping`.
sourceraw docstring

scopingclj/smacro

(scoping bindings & body)

Execute body with additional scoped bindings. Returns the value of body.

(scoping [*user-id* 123
          *request-id* "abc"]
  (ask *user-id*))  ;=> 123

Scopes can be nested; inner bindings shadow outer ones for the same var. Use skip to omit a binding and preserve normal lookup, for example:

(scoping [*user-id* (if available? user-id skip)]
  (ask *user-id* :anonymous))

nil and false still establish explicit bindings.

Execute body with additional scoped bindings. Returns the value of body.

```clojure
(scoping [*user-id* 123
          *request-id* "abc"]
  (ask *user-id*))  ;=> 123
```

Scopes can be nested; inner bindings shadow outer ones for the same var.
Use `skip` to omit a binding and preserve normal lookup, for example:

```clojure
(scoping [*user-id* (if available? user-id skip)]
  (ask *user-id* :anonymous))
```

`nil` and `false` still establish explicit bindings.
sourceraw docstring

skipclj/s

Sentinel for omitting a binding in scoping or assoc-scope.

Leaves the existing binding, or its absence, unchanged. Unlike nil and false, this value is not added to the scope.

Sentinel for omitting a binding in `scoping` or `assoc-scope`.

Leaves the existing binding, or its absence, unchanged. Unlike `nil`
and `false`, this value is not added to the scope.
sourceraw docstring

with-scopeclj/smacro

(with-scope scope & body)

Execute body with a pre-built scope map. Returns the value of body.

Unlike scoping, this takes a scope map (as returned by current-scope) rather than a bindings vector. Useful for restoring a previously captured scope in a different execution context.

Execute body with a pre-built scope map. Returns the value of body.

Unlike `scoping`, this takes a scope map (as returned by `current-scope`)
rather than a bindings vector. Useful for restoring a previously captured
scope in a different execution context.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close