A variable definition binds one or more identifiers and specifies an initial value for each of them. The simplest kind of variable definition takes one of the following forms:
Syntax: ! patternexpression
Evaluate
expression, and match the result againstpattern. Defining variables inpatternbecomes bound in the current (surrounding) scope.This is similar to
define-constantexcept generalized to apattern.
Syntax: define name [:: ] typeexpression
Evaluate the
expression, optionally converting it totype, and bind thenameto the result.
Syntax: define (name ) (formal-argumentsannotation| )option-pair* opt-return-typebody
Syntax: define (name . ) (rest-argannotation| )option-pair* opt-return-typebody
Bind the
nameto a function definition. The form:(define (nameformal-arguments)option-pair*opt-return-typebody)is equivalent to:
(definename(lambdaformal-arguments) name:nameoption-pair*opt-return-typebody))while the form:
(define (name.rest-arg)option-pair*opt-return-typebody)is equivalent to:
(definename(lambdarest-arg) name:nameoption-pair*opt-return-typebody))You can associate annotations with
name. A field annotation will be associated with the generated field; a method annotation will be associated with the generated method(s).
In addition to define (which can take an optional type specifier),
Kawa has some extra definition forms.
Syntax: define-private name [:: ] typevalue
Syntax: define-private (name formals) body
Same as
define, except thatnameis not exported.
Syntax: define-constant name [:: ] typevalue
Syntax: define-early-constant name [:: type] value
Defines
nameto have the givenvalue. The value is readonly, and you cannot assign to it. (This is not fully enforced.)If
define-early-constantis used or thevalueis a compile-time constant, then the compiler will create afinalfield with the given name and type, and evaluatevaluein the module’s class initializer (if the definition is static) or constructor (if the definition is non-static), before other definitions and expressions. Otherwise, thevalueis evaluated in the module body where it appears.If the
valueis a compile-time constant, then the definition defaults to being static.
Syntax: define-variable name [:: ] [typeinit]
If
initis specified andnamedoes not have a global variable binding, theninitis evaluated, andnamebound to the result. Otherwise, the value bound tonamedoes not change. (Note thatinitis not evaluated ifnamedoes have a global variable binding.)Also, declares to the compiler that
namewill be looked up in the per-thread dynamic environment. This can be useful for shutting up warnings from--warn-undefined-variable.This is similar to the Common Lisp
defvarform. However, the Kawa version is (currently) only allowed at module level.
For define-namespace and define-private-namespace
see Namespaces.