with

Syntax

  • with ( id := expression id := expression ... )

Parameters

  1. ididentifier
    Name of local variable.
  2. expression—(float, string, bool, float[], string[], bool[])

    Value to be stored.

Description

The with keyword successively defines a set of local variables for a definition. Within the scope of a definition local variables can be referenced using their identifier id. They are not accessible outside the scope of the definition.

Local variables can be defined for functions, attributes, const functions and rules. The expression may reference functions, attributes and const attributes which are defined outside the scope of the current definition as well as parameters and local variables which are defined prior in the list of local variables.

attr  a  with ( x := "example" )        = x

const b  with ( x := [0,1,2]  y := 0 )  = x[y]

f(a)     with ( x := a  y := x )        = y

Lot      with ( x := f(1) )             --> extrude(x)

The expression of a local variable is evaluated and its value is stored as soon as the definition is called. The expression is not being re-evaluated when a local variable is referenced.

In the following simple example a function computes and stores intermediate results in local variables d1 and d2 at the time the function is called. Each local variable is then referenced twice in order to compute the distance between 2 points.

dist2d(x1, y1, x2, y2) with ( d1 := x2-x1  
                              d2 := y2-y1 )
= sqrt(d1*d1 + d2*d2) 

Lot --> print("distance: " + dist2d(1,1,2,2)) // 1.414

The following rule computes the area of the current shape and stores it in a local variable. The value does not change despite the area of the shape is altered.

Lot with ( area := geometry.area() ) --> s('2,0,'2)
                                         print("initial area: " + area)
                                         print("altered area: " + geometry.area())


In this topic