1

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.

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Andy.Tsao
  • 11
  • 1

1 Answers1

1

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).

Ladicek
  • 5,970
  • 17
  • 20
  • thank you for your comment. but I want to know why they are serialized to different Dart_CObjects. – Andy.Tsao Sep 08 '12 at 08:45
  • What if you have new `List()` ? That is, remove the constructor parameter. Are they different CObjects? – Seth Ladd Sep 10 '12 at 06:46
  • Hi, Seth Ladd, I think that the `args` created in case 1 and the `args` created in case 2 is the same object. But in my Dart native extension, I receive different Dart_CObject. The `args` created with new List() will be serialized to the CObjects as the same as [1,2]. I think I understand the fixed-size list is different from growable list, and [1,2] will create a growable list. Thank you very much. – Andy.Tsao Sep 11 '12 at 15:42