Chapter 4

Program Control

Function Calls

General Syntax

The general syntax for function calls is

function(arg1, arg2, argn)

function has the syntax of an operand and is the function to be called. The args have the syntax of expressions, and are the arguments to the function. The function will often be a named value reference, but it can be any other kind of operand as well.

In the following example, the function being called is the value of the binding average.

average(x, y)

In the following two examples, the function being called is the value of a method statement. The examples differ only in that the second example puts parentheses around the method statement, to make the code somewhat more readable.

method(x) x + 1 end (99)
(method(x) x + 1 end) (99)

In the following examples, the function being called is the result of another function call. key-test takes a collection as an argument, and returns a predicate function. The predicate function is then applied to the two keys. The following three program fragments will have the same effect.

key-test(collection)(key1, key2)

(key-test(collection))(key1, key2)

begin
 let fun = key-test(collection);
 fun(key1, key2);
end

Functions may accept keyword arguments. These are optional and order-independent. They may also accept a variable number of rest arguments.

A complete description of functions, parameter lists, and function calling is given in Chapter 6, Functions.

Slot Reference

Dylan provides a shorthand syntax for functions that accept one argument. The syntax argument.function applies function to argument. This syntax is commonly used for slot reference, to access the function slot of argument.

Order of execution aside, the following pairs of function calls are equivalent:

america.capital
capital(america)

window.position
position(window)

Slot reference syntax can be cascaded and is left associative. Order of execution aside, the following pair of expressions are equivalent. Each returns the origin of the root-view of a window.

window.root-view.origin
origin(root-view(window))

Element Reference

Dylan provides a shorthand syntax for element reference. The syntax sequence[i] is equivalent to the function call element(sequence, i). The syntax array[i1, i2, in] is equivalent to the function call aref(array, i1, i2, in).

Order of execution aside, the following pairs of expressions are equivalent:

*all-windows*[0]
element(*all-windows*, 0)

*tic-tac-toe*[1, 2]
aref(*tic-tac-toe*, 1, 2)

The names element and aref are looked up in the environment of the element reference expression.