3.4 Side Effects

In Julia, as we have previously seen, every expression has a value. That is visible when evaluating an arithmetic expression but also when evaluating a geometric expression:

> 1+2

3

> circle(xy(1, 2), 3)

Circle(...)

In the last example, the result of evaluating the second expression is a geometric entity. When Julia writes a result that is a geometric entity it uses a notation based on the name of that entity. Most of the times, we are not interested in seeing a geometric entity as a piece of text but, instead, in visualizing that entity in space. For that purpose, the evaluation of geometric expressions also has a side effect: all created geometric shapes are automatically added to a chosen CAD application using the backend function.

That way, evaluating the following program:

using Khepri

backend(autocad)

 

circle(xy(1, 2), 3)

produces as a result an abstract value representing a circle centered at the point \(1,2\) and with a radius of \(3\) units and, as a side effect, that circle becomes visible in AutoCAD.

This behavior of geometric functions, like circle, line, rectangle, etc., is fundamentally different from the ones we have seen so far. Previously, functions were used to compute something, i.e., to produce a value, whereas now it is not the value that interests us the most but, rather, the side effect (also called collateral effect) that allows us to visualize the geometric shape in the CAD application.

One important aspect of using side effects is the possibility of their composition. The composition of side effects is accomplished through sequencing, i.e., the sequential computation of the different effects. In the next section we will discuss sequencing of side effects.