20512, "mppf", "challenges initializing an empty array of non-nilable classes", "2022-08-25T16:50:37Z"
I'd like to be able to create an array of non-nilable classes.
However, creating such an array is actually quite hard.
Here is the obvious code:
class C { }
proc main() {
var A: [1..0] owned C;
}
That fails with error: cannot default-initialize the array A because it has a non-nilable element type 'owned C'.
But, that is annoying, because the array has 0 elements, so no element needs to be default-initialized.
We have domain.unsafeAssign for initializing non-default initializable array elements. Could there be a similar feature for initializing an array? Note that I can't use it to initialize an array (because the array has to exist first in order for me to assign it...)
In the meantime, one can use ugly workarounds like this:
class C { }
proc main() {
var A: [1..0] owned C = for i in 1..0 do new C();
}
or, for an array of borrows:
class C { }
proc main() {
var dummyC: unmanaged C? = nil;
var A: [1..0] borrowed C = for i in 1..0 do dummyC!;
}