std-default


Synopsis

(std-default '<sym> <value>)

Description

STD-DEFAULT is used to initialize a variable. If the value of sym is nil, then set sym to the return value of the argument value, which may be any expression.

STD-DEFAULT should be used to initialize any variable. This is the weaker counterpart of STD-DEFAULT-TYPE, which enforces a type. STD-DEFAULT just checks for nil.

It is comparable to the Common Lisp macro DEFVAR.

Example

(std-default '*my-length* 10.0)

Note

If OR would be defined as in Lisp - to return the value of the first non-nil value - then you wouldn't need STD-DEFAULT. Instead you would use (OR sym def) within the code. You could also use (COND (sym)(def)) but this is not easily readable, therefore we use STD-DEFAULT in the function header to make our intentions clearer.

Drawback

<value> is always evaluated. So if <value> is an expensive function better use this:

(std-default 'sorted (std-sort lst)) ; not that good

=>

(if (not (boundp 'sorted)) (setq sorted (std-sort lst))) ; ugly but better

Arguments

sym: a quoted symbol, which evaluates to a symbol which needs not to be bound.

value: any valid lisp expression

Return Value

Not defined. Usually the return value of value.

Side Effects

This function is only called because of its side effect.

It changes the value of sym if sym's value is nil.

The argument <value> is always evaluated.

Module

(std-require 'STDLISP)

Defined in STDLISP