10.4 Local VariablesSometimes a sub-expression is used more than once in an expression. A so-called let expression enables you to define a local variable to represent the value of the sub-expression. The variable must be declared with a name , a type, and an expression that specifies its value. The variable may be used only in the expression following the keyword in . A let expression may be included in any OCL expression. The following example uses the model shown in Figure 10-1: context Person inv : let income : Integer = self.job.salary->sum() in if isUnemployed then income < 100 else income >= 100 endif Let expressions may be nested, but a more convenient manner of defining a number of local variables is to include them in one let expression. In that case, they must be separated by a comma: context Person inv : let income : Integer = self.job.salary->sum(), carSize : Real = self.car.engine.maxPower in if isUnemployed then income < 100 and carSize < 1.0 else income >= 100 and carSize >= 1.0 endif |