On this page:
5.1 Deferred Evaluation in Modules
5.2 Conditional Binding
5.3 Renaming Definitions
5.4 Forward Declarations
5.5 Definition Shorthands
5.6 Effectful Transformation
in-phase1/ pass2
Version: 5.0.2

5 Definitions

Carl Eastlund <cce@racket-lang.org>

 (require unstable/define)

This library is unstable; compatibility will not be maintained. See Unstable for more information.

This module provides macros for creating and manipulating definitions.

5.1 Deferred Evaluation in Modules

(at-end expr)
When used at the top level of a module, evaluates expr at the end of the module. This can be useful for calling functions before their definitions.


  > (module Failure scheme
      (f 5)
      (define (f x) x))
  > (require 'Failure)

  reference to an identifier before its definition: f in

  module: 'Failure

  > (module Success scheme
      (require unstable/define)
      (at-end (f 5))
      (define (f x) x))
  > (require 'Success)

5.2 Conditional Binding

(define-if-unbound x e)
(define-if-unbound (f . args) body ...)
(define-values-if-unbound [x ...] e)
(define-syntax-if-unbound x e)
(define-syntax-if-unbound (f . args) body ...)
(define-syntaxes-if-unbound [x ...] e)
These forms define each x (or f) if no such binding exists, or do nothing if the name(s) is(are) already bound. The define-values-if-unbound and define-syntaxes-if-unbound forms raise a syntax error if some of the given names are bound and some are not.

These are useful for writing programs that are portable across versions of Racket with different bindings, to provide an implementation of a binding for versions that do not have it but use the built-in one in versions that do.


  > (define-if-unbound x 1)
  > x


  (define y 2)
  > (define-if-unbound y 3)
  > y


5.3 Renaming Definitions

(define-renamings [new old] ...)
This form establishes a rename transformer for each new identifier, redirecting it to the corresponding old identifier.


  > (define-renamings [def define] [lam lambda])
  > (def plus (lam (x y) (+ x y)))
  > (plus 1 2)


5.4 Forward Declarations

(declare-names x ...)
This form provides forward declarations of identifiers to be defined later. It is useful for macros which expand to mutually recursive definitions, including forward references, that may be used at the Racket top level.

5.5 Definition Shorthands

(define-with-parameter name parameter)
Defines the form name as a shorthand for setting the parameter parameter. Specifically, (name value body ...) is equivalent to (parameterize ([parameter value]) body ...).


  > (define-with-parameter with-input current-input-port)
  > (with-input (open-input-string "Tom Dick Harry") (read))


(define-single-definition define-one-name define-many-name)
This form defines a marco define-one-name as a single identifier definition form with function shorthand like define and define-syntax, based on an existing macro define-many-name which works like define-values or define-syntaxes.


  > (define-single-definition define-like define-values)
  > (define-like x 0)
  > x


  > (define-like (f a b c) (printf "~s, ~s\n" a b) c)
  > (f 1 2 3)

  1, 2


5.6 Effectful Transformation

This form executes e during phase 1 (the syntax transformation phase) relative to its context, during pass 1 if it occurs in a head expansion position.

This form executes e during phase 1 (the syntax transformation phase) relative to its context, during pass 2 (after head expansion).