Initialize the length of an array whose element is class #2177
-
Hi All, I have a question and hope to get help. Thanks.
In the above code, after verification shows I don't know how to provide an initializer to the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You can initialize a fixed-length array with a sequence expression, like this:
For lengths not statically known, you can use a lambda (the parameter is the index:
|
Beta Was this translation helpful? Give feedback.
-
Section 20.16 of the reference manual gives the answer to this question. The type r := new TestType[5](_ => element); Alternatively, you can give the 5 elements explicitly, like this: r := new TestType[5] [element, element, element, element, element]; (in which case you can change |
Beta Was this translation helpful? Give feedback.
Section 20.16 of the reference manual gives the answer to this question.
The type
TestType
denotes non-null references to instances of classTestType
. When you allocate a nonempty array ofTestType
s, then these array elements need to be filled in somehow. In your case, since methodtest
already takes aTestType
argument, the easiest thing to do is to use this value to initial every element of the array. You can do that as follows:Alternatively, you can give the 5 elements explicitly, like this:
(in which case you can change
new TestType[5]
tonew TestType[]
, since Dafny will easily inf…