Text not verified for kermeta 2 | |
---|---|
Whenever you need a data locally to a block because it doesn't goes into a class attribute or reference, you can use a local variable.
A variable is defined by a name and by a type. If needed, the name of the variable can be escaped using the tilda (~)
Declaring a variable:
var foo : String // This is a variable declaration
In the following example, we define 3 variables of type integer. The first is initialized with the "14" literal value, the second is initialized with an expression using v1. For the last variable, we use a multiplicity notation to specify an ordered set of integer (see Section 2.15, “ Collections ” and Section 2.16, “ Class properties ” for more information on sets).
do var v1 : Integer init 14 var v2 : Integer init 145 * v1 var tab : Integer[0..*] init kermeta::standard::OrderedSet<Integer>.new v1 := v2/v1 end
Be careful to the multiplicity, when you create a variable with
multiplicity, you have to
initialize it with a
collection
. Then to use its
content, you need to use the collection operation
like add, addAll,
remove,
select, etc. If you use the assignment
:=
it
will replace your collection.