Reputation: 11
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
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