▼ |  | Unstable

1 | Guidelines for developing unstable libraries
2 | Bytes
3 | Classes and Objects
4 | Contracts
5 | Definitions
6 | Dictionaries
7 | Directories
8 | Exceptions
9 | Filesystem
10 | Functions
11 | Lists
12 | Net
13 | Path
14 | PLane T Packages
15 | Ports
16 | Pretty-Printing
17 | Regular Expressions
18 | Requiring Modules
19 | Sandboxed Evaluation
20 | Scribble Documentation
21 | Sets
22 | S-Expression Diff
23 | Source Locations
24 | Strings
25 | Structs
26 | Syntax
27 | Planet Package Macros
28 | Text Representations
29 | Multiple Values
30 | XML and CSS
31 | Polymorphic Contracts
32 | Finding Mutated Variables
33 | Find
34 | Interface-Oriented Programming for Classes
35 | Sequences
36 | Hash Tables
37 | Match
38 | Generics
39 | Mark Parameters
40 | Debugging
41 | Contracts for macro subexpressions
42 | Contracts for struct type properties
43 | GUI libraries

► | 5 | Definitions

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

On this page:

5.1`` Deferred Evaluation in Modules
at-end
5.2`` Conditional Binding
define-if-unbound
define-values-if-unbound
define-syntax-if-unbound
define-syntaxes-if-unbound
5.3`` Renaming Definitions
define-renamings
5.4`` Forward Declarations
declare-names
5.5`` Definition Shorthands
define-with-parameter
define-single-definition
5.6`` Effectful Transformation
in-phase1
in-phase1/ pass2

Version: 5.0.2
top ← prev up next →

### 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.

Examples:

> ( 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.

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

( define y 2)

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

#### 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.

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

#### 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 ... ).

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

( 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.

Examples:
> ( define-single-definition define-like define-values )
> (define-like x 0)
> x
0
> (define-like (f a b c) ( printf "~s, ~s\n" a b) c)
> (f 1 2 3)
1, 2
3

#### 5.6`` Effectful Transformation

( in-phase1 e)

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.

( in-phase1/pass2 e)

This form executes e during phase 1 (the syntax transformation phase) relative to its context, during pass 2 (after head expansion).
top ← prev up next →