Open
Description
In [vm/ffi] TypedData
constructors for Struct
and Union
addressing #45697, structs and unions can are automatically backed by a typed data on the default constructor.
We should consider doing similar for fixed-size arrays.
Things to consider:
- We already use the
Array
constructor for annotations.
API sketch:
void main() {
// Allocates a typed data which fits `Coordinate[4]`.
final a = Array<Coordinate>.typedData(4);
// Allocates a typed data which fits `Coordinate[2][2]`.
final a = Array<Array<Coordinate>>.typedData(2, 2);
}
final class Coordinate extends Struct {
// ...
}
If we could somehow distinguish annotation call sites from normal code, we could possibly reuse the existing constructor:
void main() {
// Allocates a typed data which fits `Coordinate[4]`.
final a = Array<Coordinate>(4);
// Allocates a typed data which fits `Coordinate[2][2]`.
final a = Array<Array<Coordinate>>(2, 2);
}
cc @mkustermann
Workaround for compile-time length arrays
After https://dart-review.googlesource.com/c/sdk/+/342763 lands.
Define a wrapper struct, allocate that as typed data, and read the array out:
main() {
final Array<MyStruct> a = HelperStruct().inlineArray;
}
class HelperStruct extends Struct {
@Array(10)
external Array<Struct> inlineArray;
}