The Break Object

The Break object is most commonly used to terminate the processing of a list of clauses in an ElseList expression or to terminate the processing of the list of clauses in a function. It's defined in the Code namespace and is typed with the Null type.

The action of the Break object is similar to the action of the "break" statement in other programming languages. Typically, you can use "break" to exit out of the processing of a "for" or "while" loop.

This is very similar to the example that we used for the Return object:

Note that TestFnV1 has three clauses, while TestFnV2 only has a single clause.

It's important to recognize that we can't write the first clause as

because when a clause returns the null object, processing just falls through to the next clause.

The object at the top of the expression tree in TestFnV2 is the BreakCatch object. The second parameter of this object is typed as Label. You don't have to create an object definition for the label objects. In the example, the A term is just Local.A. Since this object is undefined, the type inferencer will infer its type to be Label. So in this example, you can create the A term by just typing "A" and then changing the namespace to Local.

The fact that the Break object is typed as Null allows constructing expressions like:

In this clause, if "a" is the Power object, then the first term in outer ElseList would become "break: B + a". But because Break has the Null type, this reduces to just "break: B". Since the top clause in the outer ElseList has produced a result other than the null object, Test3[a] is not evaluated and the BreakCatch will cause the clause to return null. See The Null Object for more details.