The Dart Programming Language by Gilad Bracha

The Dart Programming Language by Gilad Bracha

Author:Gilad Bracha [Bracha, Gilad]
Language: eng
Format: epub, azw3
Publisher: Pearson Education
Published: 2015-12-06T23:00:00+00:00


We can write

var l = new List<String>();

l is List<String>; // true

l is List<int>; // false

One must understand the limitations of such tests in Dart however. Because the type system is unsound there is no hard guarantee that an object that claims to be, say, a List<int> contains only integers. We can be confident that the object is a list, and that it was created as a list of integers. However, any kind of object could subsequently have been inserted into it.

In checked mode however, we have a lot more confidence. Any attempt to corrupt the list by inserting an inappropriate object into it would be trapped in checked mode. Checked mode tests the actual type of objects against the declared types of variables and function results; but in a generic type, the actual type arguments to the generic are substituted for any type variables in the declared types. So, given that List defines

operator [int index]= (E e)

when one writes

Click here to view code image

var l = new List<String>();

l[0] = 'abc'; // always ok

l[1] = 42; // fails in checked mode - 42 is an int which is not a subtype of String

checked mode will ensure that uses of an instance of a generic class cannot be corrupted.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.