Clauses

Clauses are expressions that are added to object definitions when the object is intended to act as a function. These expressions define what happens when the function is executed.

Clauses typically have the form:

{ match expression } => (build expression)

but clauses aren’t required to follow this pattern. For example, an Assert expression like the one above can be used as one term in a clause rather than as the whole clause.

Consider a simple example, we define a one parameter function called Commute and we want to call that function and pass in the expression “x + y”. So we will be processing Commute[x + y]

The Commute object can be defined with a type definition Real -> Real and can contain a single clause:

If we also want to Commute to act on an expression of the form a - b, we can add a second clause:

and this function will now act on either Add or Sub objects with two parameters.

Note that clauses don't have to have an Assert object in them. For example, the autocomplete functions are often defined with just a list in them:

In this case, the List is still considered to be a clause of the function, but since there are no variables or pattern match and no executable functions are included in the expression in the clause, the clause will always evaluate to a fixed expression - in this case the List object containing the two root expressions.

The Set Default Result (Alt + D) command can be used when editing clauses in an Object Module. If the highlighted expression is parameter in a function call (for example the "a" in Simplify[a]), then this command will wrap the parameter in the DefaultResult object. In standard notation, the parameter will be displayed with angled brackets (so Simplify[a] will be displayed as Simplify[<a>]). The parameter designated as the default result will be the result of the function if the function would return null without a default result. The default result setting can also be used on functions with more than one parameter.

Disabled clauses are ignored when the function is executed, equivalent to commenting out code in other development environments. Clauses in an object definition can be disabled by right clicking and selecting Disable clause, or by selecting the clause and pressing the F9 key.

Note that disabled clauses can also be used to insert comments in functions. Ctrl + Delete with the whole clause selected will delete the expression of the clause and just leave the EmptyBox. Pressing Escape will switch to text mode, allowing you to enter whatever text you want for the comment. Comments can also include expressions and any styling you want. Press Ctrl + Enter to create a comment that has multiple lines.