Andy.Tsao
Andy.Tsao

Reputation: 11

Difference between initializing with new operator and initializing with literal in Dart

In Dart, what is the difference between initializing a List<int> with new operator and initializing it with literal?

case 1:

List<int> args = new List<int>(2);
args[0] = 1;
args[1] = 2;

case 2:

List<int> args = <int>[1, 2];

when I post the args to a native service port, the service port will receive different messages. The List instance initialized with new operator was serialized to Dart_CObject::kIntArray, but the instance initialized with literal was serialized to a Dart_CObject object with type 12.

Upvotes: 1

Views: 243

Answers (1)

Ladicek
Ladicek

Reputation: 6597

There's one tiny difference -- in the first case, you are creating a fixed-size list, while in the second case, the list is growable. If you used 'new List()' in the first case, there would be no difference. I'm not 100 % sure as I can't test it right now, but this is how I understand it (I know for sure that the VM has separate classes for fixed-size lists and growable lists).

Upvotes: 1

Related Questions