Programming Language for Old Timers


by David A. Moon
February 2006 .. September 2008

Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.


Previous page   Table of Contents   Next page


Def Statement

The def statement is the most commonly used defining statement in PLOT, hence an abbreviated name is justified. It can define one or more variables. Variables can be fixed or assignable. Variables can have declared types. The definition appears in the current scope, which can be local or global.

The term "variable" is used for traditional reasons, although most definitions are fixed, so the "variable" cannot actually vary within its scope.

The def statement has a left-hand side which specifies what to define, an = or := operator, and a right-hand side which is an expression. The value of the right-hand side becomes the value of the variable and also the value of the def statement as a whole.

The left-hand side can be a variable name followed by an optional type restriction.

The left-hand side can also be an invocation of a constructor that has a corresponding destructuring macro. The arguments to the constructor are defined as parts of the object that is the value of the right-hand side. This is how you can define multiple variables in one def statement. See the Destructuring section for details.

When the operator is :=, the variable definitions are assignable. Otherwise the variable definitions are fixed.

Annotations introduced by : can follow the operator.

The syntax of the def statement is as follows:

defmacro def ?:lhs [ ?=is ?:type-specifier] { ?= = | ??assignable ?= := }
             [ ?:annotations ] ^^ ?rhs is expression-or-lambda => ...
where the (notional) syntactic type lhs is expression but with a precedence of 180 so it will not swallow the is operator which is a bound particle introducing the type-specifier, nor the = or := operator which is a bound particle separating the left-hand and right-hand sides.

The left-hand side must parse into either a variable name or the invocation of a constructor.

If the type-specifier is present the value of the initialization expression must be a member of that type. If the definition is assignable any value assigned to the variable in the future must also be a member of that type.

The right-hand side is the initialization expression. It can be an ordinary expression or a curried function.


Previous page   Table of Contents   Next page