Is there anything like a struct in dart?

dart

Solution

That sounds like a class.

 class MyVector {
   int x;
   int y;
   MyVector(this.x, this.y);
 }

There is no simpler and more efficient way to create a name-indexed structure at runtime. For simplicity you could usually use a `Map`, but it's not as efficient as a real class.

A class should be at least as efficient (time and memory) as a fixed length list, after all it doesn't have to do an index bounds check.

In Dart 3.0, the language will introduce records. At that point, you can use a record with named fields instead of creating a primitive class:

var myVector = (x: 42, y: 37);
print(myVector.x);

A record is unmodifiable, so you won't be able to update the values after it has been created.

Problem

In javascript it always bothered me people use objects as vectors like `{x: 1, y: 2}` instead of using an array `[1,2]`. Access time for the array is much faster than the object but accessing by index is more confusing especially if you need a large array. I know dart has fixed arrays but is there a way to name the offsets of an array like you would a struct or a tuple/record in another language? Define enum/constants maybe? I'd want something like ``` List<int> myVector = new List([x,y]); myVector.x = 5; ``` is there an equivalent or idiomatic way to do this?

Original source