Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything like a struct in dart?

Tags:

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?

like image 478
BreezyChick89 Avatar asked Jul 15 '14 15:07

BreezyChick89


People also ask

What are the data structures in Dart?

Basic Data Structures in Dart These include `List`, `Map` and `Set`. Understanding how they function will give you a foundation to work from as you proceed through the book and begin creating your own data structures from scratch.

Is Dart strong typed?

🎯 Dart is strongly-typed language. When using Firestore, a NoSQL database, be careful with accessing fields in a document to ensure it is the expected type, & to handle nulls.

Is everything an object in Dart?

Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects. With the exception of null (if you enable sound null safety), all objects inherit from the Object class. Version note: Null safety was introduced in Dart 2.12.

How do you stack in darts?

Creating a Dart stack // create a dynamic stack to hold data of any type Stack dynamicStack = Stack(); // create a stack of int to hold int values Stack<int> intStack = Stack();


1 Answers

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.

like image 100
lrn Avatar answered Sep 22 '22 02:09

lrn