Macros & Constants¶
Simplifying Path Expressions with Macros¶
When managing extensive product hierarchies, path expressions are frequently used and can become cumbersome.
To streamline the process, the COOM language incorporates the define
keyword to support macros.
This feature allows for the creation of local macro variables, simplifying the recurrent use of lengthy path expressions.
For instance, a macro can be defined to represent the path expression that specifies the bag color.
product {
Color color
...
Carrier carrier
}
structure Carrier {
Color carrierColor
Bag bag
}
structure Bag {
Capacity capacity
Material material
Color color
}
// 'define' introduces a macro for the carrier's bag color
behavior {
define bagColor = root.carrier.bag.color
require color = bagColor
}
Here, we define a behavior stating that the bike's color needs to be the same as the color of the bags (included in the carrier
structure).
Note that the variables declared by the define
keyword are only visible/available in the scope of the current behavior block.
If you define a macro inside a block { ... }
, it is only visible/available inside that block.
Constants and Formulas¶
The define
keyword can also be used to specify constants or formulas.
In the following example the constant PI is defined for being used in calculations:
structure Circle {
num radius
num area
num circumference
}
behavior Circle {
define PI = 3.1415
define PI_2 = PI * 2
imply area = radius^2 * PI
imply circumference = radius * PI_2
}
It is worth noticing, that a define statement can be used in the definition of another define statement (see PI
and PI_2
).