10.4 Local Variables


10.4 Local Variables

Sometimes 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 


Object Constraint Language, The. Getting Your Models Ready for MDA
The Object Constraint Language: Getting Your Models Ready for MDA (2nd Edition)
ISBN: 0321179366
EAN: 2147483647
Year: 2003
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net