Next section: Statement bodiesExpressions
Dylan programs are composed of expressions. When an expression is evaluated, it returns zero or more values. Evaluating an expression may have side effects and may involve evaluating sub-expressions.An outer expression is an expression that is not a sub-expression of any expression. A top-level expression is either an outer expression or a direct sub-expression of a begin syntax form that itself is a top-level expression. (Such a begin cannot have local declarations in it, although the grammar allows it.) Definitions (see Syntax Forms, below) are restricted to be top-level expressions. Other expressions may appear either as top-level expressions, or as sub-expressions of other expressions.
There are four kinds of expressions: literal constants, variable references, function calls, and syntax forms.[8]
Literal Constants
A literal constant is an object that is specified explicitly in program text.? "abc" "abc" ? 123 123 ? foo: foo: ? 'M' 'M' ? #t #t ? #f #f ? #(1, 2, 3) #(1, 2, 3)Literal constants are immutable. The keys and elements of collections that are literal constants are immutable. Attempting to modify an immutable object has undefined consequences. Immutable objects may share structure. Literal constants that are = may or may not be ==.Variable References
When an expression is a variable name, the expression indicates a variable reference. The variable name evaluates to the value of the variable.Dylan supports two kinds of variables: lexical variables and module variables. Lexical variables are created locally. They roughly correspond to local variables in other languages. Module variables are created by using a definition such as define variable or define method. They roughly correspond to global variables in other languages.
? <window> {the class <window>} ? concatenate {the generic function concatenate} ? define variable my-variable = 25; my-variable ? my-variable 25 ? begin let x = 50; x + x; end; 100Note that Dylan classes and functions are first-class objects and are named by variables, like other objects.See the chapter on Variables for detailed information about lexical and module variables.
Function Calls
Function calls generally have the syntaxexpression(arg1, arg2, ... argn)
expression must evaluate to a function. This is the function to be called. The arguments to the function are the values of the elements in the list.
The arguments to a function are evaluated in left-to-right order. The function is evaluated before the arguments.[9] Once the function and all the arguments are evaluated, the function is called with the arguments.
A function call evaluates to the values returned by the function.
expression is commonly a variable name. In the following example, the function being called is the value of the variable factorial:
factorial(x, y)However, the expression in the function position does not have to be a variable reference; it can be any expression that evaluates to a function. In this way, Dylan is like C and Scheme, and unlike Common Lisp. The following example has a complex expression in the "function position." The complex expression creates and returns a function which adds one to its argument. This function is then applied to 99.? (method(x) x + 1 end) (99) 100Syntax Forms
A syntax form is a form which has its own rule for evaluation. Syntax forms are introduced by syntax operators. Some examples of syntax operators are define variable, for, method, and :=.Syntax forms may have different evaluation rules than function calls. In a function call, all the subexpressions are evaluated and passed to the function as arguments. In contrast, a syntax form can examine its subexpressions literally and can choose to evaluate some and give special meaning to others. The special evaluation rules for each syntax form are listed along with that form's definition.
Generally, the syntax operator is the first word or words appearing in the syntax form. For example, the define variable syntax form begins with the words define variable, and the method syntax form begins with the word method.
define variable foo = 3; method (a :: <number>, b :: <number>) list (a - b, a + b); end method; // Creates an anonymous method, which expects 2 // numeric arguments.However, the syntax operator does not have to be the first word in the form. For example, the assignment operator, :=, is an infix operator that is also a syntax form with special evaluation rules.my-variable := 12; // Sets the value to 12There are three kinds of syntax forms:* Most syntax forms are macros (introduced by macro operators). Some macros (like the for iteration construct) are part of the core Dylan language. Others are created by individual implementations or by programmers. Macros can be thought of as expanding into other, equivalent, expressions.
* Special forms (introduced by special form operators) are basic, built-in syntax forms that cannot be reduced to other expressions. Examples are := and method.
* Definitions (introduced by definition operators) are declarative parts of a program. Definitions are restricted to be top level expressions (see the next section). They do not return values. Examples are define method and define variable.