Skip to content

Initialize the length of an array whose element is class #2177

Answered by RustanLeino
dreamqin68 asked this question in Q&A
Discussion options

You must be logged in to vote

Section 20.16 of the reference manual gives the answer to this question.

The type TestType denotes non-null references to instances of class TestType. When you allocate a nonempty array of TestTypes, then these array elements need to be filled in somehow. In your case, since method test already takes a TestType argument, the easiest thing to do is to use this value to initial every element of the array. You can do that as follows:

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 new TestType[5] to new TestType[], since Dafny will easily inf…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@dreamqin68
Comment options

Comment options

You must be logged in to vote
1 reply
@dreamqin68
Comment options

Answer selected by dreamqin68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants