| OLD | NEW |
| 1 --- | 1 --- |
| 2 # WARNING: GENERATED FILE. DO NOT EDIT. | 2 # WARNING: GENERATED FILE. DO NOT EDIT. |
| 3 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book | 3 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book |
| 4 layout: book | 4 layout: book |
| 5 title: "Chapter 3. A Tour of the Dart Libraries from Dart: Up and Running" | 5 title: "Chapter 3. A Tour of the Dart Libraries from Dart: Up and Running" |
| 6 description: "Read Chapter 3. A Tour of the Dart Libraries of Dart: Up and Runni
ng, published by O'Reilly." | 6 description: "Read Chapter 3. A Tour of the Dart Libraries of Dart: Up and Runni
ng, published by O'Reilly." |
| 7 --- | 7 --- |
| 8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
olspan="3" align="center">Chapter 3. A Tour of the Dart Libraries</th></tr><tr><
td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th
width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
href="ch04.html">Next</a></td></tr></table><hr></div><div class="chapter"><div
class="titlepage"><div><div><h1 class="title"><a name="ch03"></a>Chapter 3. A To
ur of the Dart Libraries</h1></div></div></div><div class="toc"><p><b>Table of C
ontents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch03.html#ch03-d
artcore---strings-collections-and-more">dart:core - Numbers, Collections, String
s, and More</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch
03-numbers">Numbers</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch
03-strings-and-regular-expressions">Strings and Regular Expressions</a></span></
dt><dt><span class="sect2"><a href="ch03.html#ch03-collections">Collections</a><
/span></dt><dt><span class="sect2"><a href="ch03.html#ch03-uri">URIs</a></span><
/dt><dt><span class="sect2"><a href="ch03.html#ch03-dates-and-times">Dates and T
imes</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-utility-inte
rfaces">Utility Classes</a></span></dt><dt><span class="sect2"><a href="ch03.htm
l#ch03-exceptions">Exceptions</a></span></dt></dl></dd><dt><span class="sect1"><
a href="ch03.html#ch03-asynchronous-programming">dart:async - Asynchronous Progr
amming</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-fu
ture-example">Using Future and Completer</a></span></dt><dt><span class="sect2">
<a href="ch03.html#ch03-future-error-handling">Error Handling with Futures</a></
span></dt><dt><span class="sect2"><a href="ch03.html#ch03-chaining-multiple-asyn
c-methods">Chaining Multiple Asynchronous Methods</a></span></dt><dt><span class
="sect2"><a href="ch03.html#ch03-waiting-for-multiple-futures">Waiting for Multi
ple Futures</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-
information-9">More Information</a></span></dt></dl></dd><dt><span class="sect1"
><a href="ch03.html#ch03-dart-math">dart:math - Math and Random</a></span></dt><
dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-trigonometry">Trigonometr
y</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-maximum-and-min
imum">Maximum and Minimum</a></span></dt><dt><span class="sect2"><a href="ch03.h
tml#ch03-math-constants">Math Constants</a></span></dt><dt><span class="sect2"><
a href="ch03.html#ch03-random-numbers">Random Numbers</a></span></dt><dt><span c
lass="sect2"><a href="ch03.html#ch03-more-information-7">More Information</a></s
pan></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dart-html-usi
ng-html5-apis">dart:html - Browser-Based Apps</a></span></dt><dd><dl><dt><span c
lass="sect2"><a href="ch03.html#ch03-manipulating-the-dom">Manipulating the DOM<
/a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-httprequest">Usin
g HTTP Resources with HttpRequest</a></span></dt><dt><span class="sect2"><a href
="ch03.html#ch03-websockets">Sending and Receiving Real-Time Data with WebSocket
s</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-informatio
n-html">More information</a></span></dt></dl></dd><dt><span class="sect1"><a hre
f="ch03.html#ch03-dartisolate---concurrency-with-isolates">dart:isolate - Concur
rency with Isolates</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03
.html#ch03-isolate-concepts">Isolate Concepts</a></span></dt><dt><span class="se
ct2"><a href="ch03.html#ch03-using-isolates">Using Isolates</a></span></dt><dt><
span class="sect2"><a href="ch03.html#ch03-more-information-11">More Information
</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dartio
---file-and-socket-io-for-command-line-apps">dart:io - I/O for Command-Line Apps
</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-files-an
d-directories">Files and Directories</a></span></dt><dt><span class="sect2"><a h
ref="ch03.html#ch03-io-http">HTTP Clients and Servers</a></span></dt></dl></dd><
dt><span class="sect1"><a href="ch03.html#ch03-json">dart:json - Encoding and De
coding Objects</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html
#ch03-json-decoding">Decoding JSON</a></span></dt><dt><span class="sect2"><a hre
f="ch03.html#ch03-json-encoding">Encoding JSON</a></span></dt></dl></dd><dt><spa
n class="sect1"><a href="ch03.html#ch03-utf">dart:utf - Strings and Unicode</a><
/span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-utf-decode-ut
f8">Decoding UTF-8 Characters</a></span></dt><dt><span class="sect2"><a href="ch
03.html#ch03-utf-encode-utf8">Encoding Strings to UTF-8 Bytes</a></span></dt><dt
><span class="sect2"><a href="ch03.html#ch03-utf-other">Other Functionality</a><
/span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-mirrors">da
rt:mirrors - Reflection</a></span></dt><dd><dl><dt><span class="sect2"><a href="
ch03.html#ch03-mirrors-symbols">Symbols</a></span></dt><dt><span class="sect2"><
a href="ch03.html#ch03-mirrors-introspection">Introspection</a></span></dt><dt><
span class="sect2"><a href="ch03.html#ch03-mirrors-invocation">Invocation</a></s
pan></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-summary">Summ
ary</a></span></dt></dl></div><p>This chapter shows you how to use the major fea
tures in Dart’s | 8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
olspan="3" align="center">Chapter 3. A Tour of the Dart Libraries</th></tr><tr><
td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th
width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
href="ch04.html">Next</a></td></tr></table><hr></div><div class="chapter"><div
class="titlepage"><div><div><h1 class="title"><a name="ch03"></a>Chapter 3. A To
ur of the Dart Libraries</h1></div></div></div><div class="toc"><p><b>Table of C
ontents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch03.html#ch03-d
artcore---strings-collections-and-more">dart:core - Numbers, Collections, String
s, and More</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch
03-numbers">Numbers</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch
03-strings-and-regular-expressions">Strings and Regular Expressions</a></span></
dt><dt><span class="sect2"><a href="ch03.html#ch03-collections">Collections</a><
/span></dt><dt><span class="sect2"><a href="ch03.html#ch03-uri">URIs</a></span><
/dt><dt><span class="sect2"><a href="ch03.html#ch03-dates-and-times">Dates and T
imes</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-utility-inte
rfaces">Utility Classes</a></span></dt><dt><span class="sect2"><a href="ch03.htm
l#ch03-exceptions">Exceptions</a></span></dt></dl></dd><dt><span class="sect1"><
a href="ch03.html#ch03-asynchronous-programming">dart:async - Asynchronous Progr
amming</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-fu
ture-example">Using Future and Completer</a></span></dt><dt><span class="sect2">
<a href="ch03.html#ch03-future-error-handling">Error Handling with Futures</a></
span></dt><dt><span class="sect2"><a href="ch03.html#ch03-chaining-multiple-asyn
c-methods">Chaining Multiple Asynchronous Methods</a></span></dt><dt><span class
="sect2"><a href="ch03.html#ch03-waiting-for-multiple-futures">Waiting for Multi
ple Futures</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-
information-9">More Information</a></span></dt></dl></dd><dt><span class="sect1"
><a href="ch03.html#ch03-dart-math">dart:math - Math and Random</a></span></dt><
dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-trigonometry">Trigonometr
y</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-maximum-and-min
imum">Maximum and Minimum</a></span></dt><dt><span class="sect2"><a href="ch03.h
tml#ch03-math-constants">Math Constants</a></span></dt><dt><span class="sect2"><
a href="ch03.html#ch03-random-numbers">Random Numbers</a></span></dt><dt><span c
lass="sect2"><a href="ch03.html#ch03-more-information-7">More Information</a></s
pan></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dart-html-usi
ng-html5-apis">dart:html - Browser-Based Apps</a></span></dt><dd><dl><dt><span c
lass="sect2"><a href="ch03.html#ch03-manipulating-the-dom">Manipulating the DOM<
/a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-httprequest">Usin
g HTTP Resources with HttpRequest</a></span></dt><dt><span class="sect2"><a href
="ch03.html#ch03-websockets">Sending and Receiving Real-Time Data with WebSocket
s</a></span></dt><dt><span class="sect2"><a href="ch03.html#ch03-more-informatio
n-html">More information</a></span></dt></dl></dd><dt><span class="sect1"><a hre
f="ch03.html#ch03-dartisolate---concurrency-with-isolates">dart:isolate - Concur
rency with Isolates</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03
.html#ch03-isolate-concepts">Isolate Concepts</a></span></dt><dt><span class="se
ct2"><a href="ch03.html#ch03-using-isolates">Using Isolates</a></span></dt><dt><
span class="sect2"><a href="ch03.html#ch03-more-information-11">More Information
</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-dartio
---file-and-socket-io-for-command-line-apps">dart:io - I/O for Command-Line Apps
</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-files-an
d-directories">Files and Directories</a></span></dt><dt><span class="sect2"><a h
ref="ch03.html#ch03-io-http">HTTP Clients and Servers</a></span></dt></dl></dd><
dt><span class="sect1"><a href="ch03.html#ch03-json">dart:json - Encoding and De
coding Objects</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html
#ch03-json-decoding">Decoding JSON</a></span></dt><dt><span class="sect2"><a hre
f="ch03.html#ch03-json-encoding">Encoding JSON</a></span></dt></dl></dd><dt><spa
n class="sect1"><a href="ch03.html#ch03-utf">dart:utf - Strings and Unicode</a><
/span></dt><dd><dl><dt><span class="sect2"><a href="ch03.html#ch03-utf-decode-ut
f8">Decoding UTF-8 Characters</a></span></dt><dt><span class="sect2"><a href="ch
03.html#ch03-utf-encode-utf8">Encoding Strings to UTF-8 Bytes</a></span></dt><dt
><span class="sect2"><a href="ch03.html#ch03-utf-other">Other Functionality</a><
/span></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-mirrors">da
rt:mirrors - Reflection</a></span></dt><dd><dl><dt><span class="sect2"><a href="
ch03.html#ch03-mirrors-symbols">Symbols</a></span></dt><dt><span class="sect2"><
a href="ch03.html#ch03-mirrors-introspection">Introspection</a></span></dt><dt><
span class="sect2"><a href="ch03.html#ch03-mirrors-invocation">Invocation</a></s
pan></dt></dl></dd><dt><span class="sect1"><a href="ch03.html#ch03-summary">Summ
ary</a></span></dt></dl></div><p>This chapter shows you how to use the major fea
tures in Dart’s |
| 9 libraries. It’s just an overview, and by no means comprehensive. Whenever | 9 libraries. It’s just an overview, and by no means comprehensive. Whenever |
| 10 you need more details about a class, consult the <a class="ulink" href="http:/
/api.dartlang.org/" target="_top">Dart API reference. </a> Expect major | 10 you need more details about a class, consult the <a class="ulink" href="http:/
/api.dartlang.org/" target="_top">Dart API reference. </a> Expect major |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // Get all the characters in the string as a list of UTF-16 code units. | 80 // Get all the characters in the string as a list of UTF-16 code units. |
| 81 // Some characters might require two code units. | 81 // Some characters might require two code units. |
| 82 var codeUnitList = 'Never odd or even'.codeUnits.toList(); | 82 var codeUnitList = 'Never odd or even'.codeUnits.toList(); |
| 83 assert(codeUnitList[0] == 78);</pre></div><div class="sect3"><div class="titlepa
ge"><div><div><h4 class="title"><a name="ch03-converting-to-uppercase-or-lowerca
se"></a>Converting to uppercase or lowercase</h4></div></div></div><p>You can ea
sily convert strings to their uppercase and lowercase | 83 assert(codeUnitList[0] == 78);</pre></div><div class="sect3"><div class="titlepa
ge"><div><div><h4 class="title"><a name="ch03-converting-to-uppercase-or-lowerca
se"></a>Converting to uppercase or lowercase</h4></div></div></div><p>You can ea
sily convert strings to their uppercase and lowercase |
| 84 variants:</p><pre class="screen"><em><span class="remark">lang-dart | 84 variants:</p><pre class="screen"><em><span class="remark">lang-dart |
| 85 </span></em><em><span class="remark">ch03/string-tests.dart | 85 </span></em><em><span class="remark">ch03/string-tests.dart |
| 86 </span></em>// Convert to uppercase. | 86 </span></em>// Convert to uppercase. |
| 87 assert('structured web apps'.toUpperCase() == 'STRUCTURED WEB APPS'); | 87 assert('structured web apps'.toUpperCase() == 'STRUCTURED WEB APPS'); |
| 88 | 88 |
| 89 // Convert to lowercase. | 89 // Convert to lowercase. |
| 90 assert('STRUCTURED WEB APPS'.toLowerCase() == 'structured web apps');</pre></div
><div class="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="
ch03-trimming-and-empty-strings"></a>Trimming and empty strings</h4></div></div>
</div><p>Remove all leading and trailing white space with | 90 assert('STRUCTURED WEB APPS'.toLowerCase() == 'structured web apps');</pre><div
class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">
Note</h3><p>These methods don't work for every language. For example, the |
| 91 Turkish alphabet's dotless <span class="emphasis"><em>I</em></span> is
converted |
| 92 incorrectly.</p></div></div><div class="sect3"><div class="titlepage">
<div><div><h4 class="title"><a name="ch03-trimming-and-empty-strings"></a>Trimmi
ng and empty strings</h4></div></div></div><p>Remove all leading and trailing wh
ite space with |
| 91 <code class="literal">trim()</code>. To check whether a string is empty
(length | 93 <code class="literal">trim()</code>. To check whether a string is empty
(length |
| 92 is zero), use <code class="literal">isEmpty</code>.</p><pre class="scree
n"><em><span class="remark">lang-dart | 94 is zero), use <code class="literal">isEmpty</code>.</p><pre class="scree
n"><em><span class="remark">lang-dart |
| 93 </span></em><em><span class="remark">ch03/string-tests.dart | 95 </span></em><em><span class="remark">ch03/string-tests.dart |
| 94 </span></em>// Trim a string. | 96 </span></em>// Trim a string. |
| 95 assert(' hello '.trim() == 'hello'); | 97 assert(' hello '.trim() == 'hello'); |
| 96 | 98 |
| 97 // Check whether a string is empty. | 99 // Check whether a string is empty. |
| 98 assert(''.isEmpty); | 100 assert(''.isEmpty); |
| 99 | 101 |
| 100 // Strings with only white space are not empty. | 102 // Strings with only white space are not empty. |
| (...skipping 1318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1419 assert(() { | 1421 assert(() { |
| 1420 if (encoded.length != expected.length) return false; | 1422 if (encoded.length != expected.length) return false; |
| 1421 for (int i = 0; i < encoded.length; i++) { | 1423 for (int i = 0; i < encoded.length; i++) { |
| 1422 if (encoded[i] != expected[i]) return false; | 1424 if (encoded[i] != expected[i]) return false; |
| 1423 } | 1425 } |
| 1424 return true; | 1426 return true; |
| 1425 }); | 1427 }); |
| 1426 }</pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="titl
e"><a name="ch03-utf-other"></a>Other Functionality</h3></div></div></div><p>The
UTF library can decode and encode UTF-16 and UTF-32 bytes. The | 1428 }</pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="titl
e"><a name="ch03-utf-other"></a>Other Functionality</h3></div></div></div><p>The
UTF library can decode and encode UTF-16 and UTF-32 bytes. The |
| 1427 library can also convert directly to and from Unicode code points and | 1429 library can also convert directly to and from Unicode code points and |
| 1428 UTF8-encoded bytes. For details, see the <a class="ulink" href="http://api
.dartlang.org/utf.html" target="_top">API docs for the UTF | 1430 UTF8-encoded bytes. For details, see the <a class="ulink" href="http://api
.dartlang.org/utf.html" target="_top">API docs for the UTF |
| 1429 library.</a></p></div></div><div class="sect1"><div class="titlepage"><div
><div><h2 class="title" style="clear: both"><a name="ch03-mirrors"></a>dart:mirr
ors - Reflection</h2></div></div></div><p>The mirrors library provides basic ref
lection abilities to Dart. Use mirrors | 1431 library.</a></p></div></div><div class="sect1"><div class="titlepage"><div
><div><h2 class="title" style="clear: both"><a name="ch03-mirrors"></a>dart:mirr
ors - Reflection</h2></div></div></div><p>The mirrors library provides basic ref
lection abilities to Dart. Use |
| 1430 to query the structure of your program, and to dynamically invoke function
s or | 1432 mirrors to query the structure of your program, and to dynamically invoke |
| 1431 methods at runtime.</p><p>Mirrors can give you information about objects a
nd classes inside the | 1433 functions or methods at runtime.</p><p>Mirrors can give you information abou
t objects and classes inside |
| 1432 current isolate, as well as in other (possibly remote) isolates. Therefore
, | 1434 the current isolate, as well as in other (possibly remote) isolates. |
| 1433 the mirrors library often has two versions of each method: | 1435 Therefore, the mirrors library often has two versions of each method: |
| 1434 synchronous (for working inside the current isolate) and asynchronous | 1436 synchronous (for working inside the current isolate) and asynchronous (for |
| 1435 (for working with another isolate). | 1437 working with another isolate). This section covers mirrors only as they |
| 1436 This section covers mirrors only as they apply to the | 1438 apply to the current isolate.</p><div class="note" style="margin-left: 0.5in
; margin-right: 0.5in;"><h3 class="title">Note</h3><p>Mirrors can also work on s
tatic source code. For example, the |
| 1437 current isolate.</p><div class="note" style="margin-left: 0.5in; margin-ri
ght: 0.5in;"><h3 class="title">Note</h3><p> Mirrors can also work on static sour
ce code. | 1439 dartdoc tool (which generates HTML docs from Dart source code) uses |
| 1438 For example, the dartdoc tool | 1440 source-code mirrors.</p></div><div class="sect2"><div class="titlepage"><d
iv><div><h3 class="title"><a name="ch03-mirrors-symbols"></a>Symbols</h3></div><
/div></div><p>The mirror system represents the names of Dart declarations |
| 1439 (which generates HTML docs from Dart source code) | 1441 (classes, fields, and so on) by instances of the class <a class="ulink" hr
ef="http://api.dartlang.org/dart_core/Symbol.html" target="_top">Symbol</a>. |
| 1440 uses source-code mirrors. | 1442 Symbols help compilers like dart2js produce efficient output, even when |
| 1441 </p></div><div class="sect2"><div class="titlepage"><div><div><h3 clas
s="title"><a name="ch03-mirrors-symbols"></a>Symbols</h3></div></div></div><p>Th
e mirror system represents the names of Dart declarations | 1443 minified.</p><p>When you know the name of the symbol ahead of time, use Sy
mbol's |
| 1442 (classes, fields, and so on) by instances of the class | 1444 const constructor. This way, repeated uses of the same symbol can use |
| 1443 <a class="ulink" href="http://api.dartlang.org/dart_core/Symbol.html" ta
rget="_top">Symbol</a>. | 1445 the same canonicalized instance. If the name of the symbol is determined |
| 1444 Symbols help compilers like dart2js produce efficient output, | 1446 dynamically at runtime, use the factory constructor.</p><pre class="screen
"><em><span class="remark">lang-dart |
| 1445 even when minified.</p><p>When you know the name of the symbol ahead of
time, | |
| 1446 use Symbol's const constructor. | |
| 1447 This way, repeated uses of the same symbol can use the same | |
| 1448 canonicalized instance. | |
| 1449 If the name of the symbol is determined dynamically at runtime, | |
| 1450 use the factory constructor.</p><pre class="screen"><em><span class="rem
ark">lang-dart | |
| 1451 </span></em>// If the symbol name is known at compile time. | 1447 </span></em>// If the symbol name is known at compile time. |
| 1452 const className = const Symbol('MyClass'); | 1448 const className = const Symbol('MyClass'); |
| 1453 | 1449 |
| 1454 // If the symbol name is dynamically determined. | 1450 // If the symbol name is dynamically determined. |
| 1455 var userInput = askUserForNameOfFunction(); | 1451 var userInput = askUserForNameOfFunction(); |
| 1456 var functionName = new Symbol(userInput);</pre><p>During minification, a compile
r might replace a Symbol name | 1452 var functionName = new Symbol(userInput);</pre><p>During minification, a compile
r might replace a Symbol name with a |
| 1457 with a different (often smaller) name. | 1453 different (often smaller) name. To convert from a symbol back to a |
| 1458 To convert from a symbol back to a string, use | 1454 string, use <code class="literal">MirrorSystem.getName()</code>. This func
tion |
| 1459 <code class="literal">MirrorSystem.getName()</code>. This function retur
ns | 1455 returns the correct name, even if the code was minified.</p><pre class="sc
reen"><em><span class="remark">lang-dart |
| 1460 the correct name, even if the code was minified.</p><pre class="screen">
<em><span class="remark">lang-dart | |
| 1461 </span></em>import 'dart:mirrors'; | 1456 </span></em>import 'dart:mirrors'; |
| 1462 | 1457 |
| 1463 main() { | 1458 main() { |
| 1464 const name = const Symbol('MyClass'); | 1459 const name = const Symbol('MyClass'); |
| 1465 | 1460 |
| 1466 assert('MyClass' == MirrorSystem.getName(name)); | 1461 assert('MyClass' == MirrorSystem.getName(name)); |
| 1467 }</pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="titl
e"><a name="ch03-mirrors-introspection"></a>Introspection</h3></div></div></div>
<p>Use mirrors to introspect the running program's structure. You can | 1462 }</pre></div><div class="sect2"><div class="titlepage"><div><div><h3 class="titl
e"><a name="ch03-mirrors-introspection"></a>Introspection</h3></div></div></div>
<p>Use mirrors to introspect the running program's structure. You can |
| 1468 inspect classes, libraries, instances, and more.</p><p>The examples in t
his section use the following Person class:</p><pre class="screen"><em><span cla
ss="remark">lang-dart | 1463 inspect classes, libraries, instances, and more.</p><p>The examples in thi
s section use the following Person |
| 1464 class:</p><pre class="screen"><em><span class="remark">lang-dart |
| 1469 </span></em>class Person { | 1465 </span></em>class Person { |
| 1470 String firstName; | 1466 String firstName; |
| 1471 String lastName; | 1467 String lastName; |
| 1472 int age; | 1468 int age; |
| 1473 | 1469 |
| 1474 Person(this.firstName, this.lastName, this.age); | 1470 Person(this.firstName, this.lastName, this.age); |
| 1475 | 1471 |
| 1476 String get fullName => '$firstName $lastName'; | 1472 String get fullName => '$firstName $lastName'; |
| 1477 | 1473 |
| 1478 void greet(String other) { | 1474 void greet(String other) { |
| 1479 print('Hello there, $other!'); | 1475 print('Hello there, $other!'); |
| 1480 } | 1476 } |
| 1481 }</pre><p>To begin, you | 1477 }</pre><p>To begin, you need to <span class="emphasis"><em>reflect</em></span> o
n a class or |
| 1482 need to <span class="emphasis"><em>reflect</em></span> on a class or obj
ect to | 1478 object to get its <span class="emphasis"><em>mirror</em></span>.</p><div c
lass="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-mi
rrors-class-inspect"></a>Class Mirrors</h4></div></div></div><p>Reflect on a Typ
e to get its ClassMirror.</p><pre class="screen"><em><span class="remark">lang-d
art |
| 1483 get its <span class="emphasis"><em>mirror</em></span>.</p><div class="se
ct3"><div class="titlepage"><div><div><h4 class="title"><a name="ch03-mirrors-cl
ass-inspect"></a>Class Mirrors</h4></div></div></div><p>Reflect on a Type to get
its ClassMirror.</p><pre class="screen"><em><span class="remark">lang-dart | |
| 1484 </span></em>ClassMirror mirror = reflectClass(Person); | 1479 </span></em>ClassMirror mirror = reflectClass(Person); |
| 1485 | 1480 |
| 1486 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>You can als
o call <code class="literal">runtimeType</code> | 1481 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>You can als
o call <code class="literal">runtimeType</code> to get a Type |
| 1487 to get a Type from an instance.</p><pre class="screen"><em><span class
="remark">lang-dart | 1482 from an instance.</p><pre class="screen"><em><span class="remark">lang-d
art |
| 1488 </span></em>var bob = new Person('Bob', 'Smith', 33); | 1483 </span></em>var bob = new Person('Bob', 'Smith', 33); |
| 1489 ClassMirror mirror = reflectClass(bob.runtimeType); | 1484 ClassMirror mirror = reflectClass(bob.runtimeType); |
| 1490 | 1485 |
| 1491 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>Once you ha
ve a ClassMirror, you can get a class's | 1486 assert('Person' == MirrorSystem.getName(mirror.simpleName));</pre><p>Once you ha
ve a ClassMirror, you can get a class's constructors, |
| 1492 constructors, fields, and more. Here is an example of listing | 1487 fields, and more. Here is an example of listing the constructors of a |
| 1493 the constructors of a class.</p><pre class="screen"><em><span class="r
emark">lang-dart | 1488 class.</p><pre class="screen"><em><span class="remark">lang-dart |
| 1494 </span></em>showConstructors(ClassMirror mirror) { | 1489 </span></em>showConstructors(ClassMirror mirror) { |
| 1495 Map<Symbol, MethodMirror> constructors = mirror.constructors; | 1490 Map<Symbol, MethodMirror> constructors = mirror.constructors; |
| 1496 | 1491 |
| 1497 constructors.forEach((s, m) { | 1492 constructors.forEach((s, m) { |
| 1498 print('The constructor $s has ${m.parameters.length} parameters.'); | 1493 print('The constructor $s has ${m.parameters.length} parameters.'); |
| 1499 }); | 1494 }); |
| 1500 }</pre><p>Here is an example of listing all of the fields declared by a | 1495 }</pre><p>Here is an example of listing all of the fields declared by a |
| 1501 class.</p><pre class="screen"><em><span class="remark">lang-dart | 1496 class.</p><pre class="screen"><em><span class="remark">lang-dart |
| 1502 </span></em>showFields(ClassMirror mirror) { | 1497 </span></em>showFields(ClassMirror mirror) { |
| 1503 Map<Symbol, VariableMirror> fields = mirror.variables; | 1498 Map<Symbol, VariableMirror> fields = mirror.variables; |
| 1504 | 1499 |
| 1505 fields.forEach((s, v) { | 1500 fields.forEach((s, v) { |
| 1506 var finalStatus = v.isFinal ? 'final' : 'not final'; | 1501 var finalStatus = v.isFinal ? 'final' : 'not final'; |
| 1507 var privateStatus = v.isPrivate ? 'private' : 'not private'; | 1502 var privateStatus = v.isPrivate ? 'private' : 'not private'; |
| 1508 var typeAnnotation = v.type.simpleName; | 1503 var typeAnnotation = v.type.simpleName; |
| 1509 | 1504 |
| 1510 print('The field $s is $privateStatus and $finalStatus and is annotated ' | 1505 print('The field $s is $privateStatus and $finalStatus and is annotated ' |
| 1511 'as $typeAnnotation'); | 1506 'as $typeAnnotation'); |
| 1512 }); | 1507 }); |
| 1513 }</pre><p>For a full list of methods, | 1508 }</pre><p>For a full list of methods, consult the <a class="ulink" href="http://
api.dartlang.org/dart_mirrors/ClassMirror.html" target="_top">API docs |
| 1514 consult the <a class="ulink" href="http://api.dartlang.org/dart_mirror
s/ClassMirror.html" target="_top">API docs | 1509 for ClassMirror</a>.</p></div><div class="sect3"><div class="titlepage">
<div><div><h4 class="title"><a name="ch03-mirrors-instance-inspect"></a>Instance
Mirrors</h4></div></div></div><p>Reflect on an object to get an InstanceMirror.
</p><pre class="screen"><em><span class="remark">lang-dart |
| 1515 for ClassMirror</a>.</p></div><div class="sect3"><div class="titlepage
"><div><div><h4 class="title"><a name="ch03-mirrors-instance-inspect"></a>Instan
ce Mirrors</h4></div></div></div><p>Reflect on an object to get an InstanceMirro
r.</p><pre class="screen"><em><span class="remark">lang-dart | |
| 1516 </span></em>reflectOnInstance() { | 1510 </span></em>reflectOnInstance() { |
| 1517 var p = new Person('Bob', 'Smith', int); | 1511 var p = new Person('Bob', 'Smith', int); |
| 1518 InstanceMirror mirror = reflect(p); | 1512 InstanceMirror mirror = reflect(p); |
| 1519 }</pre><p>If you have an InstanceMirror and you want to get the object | 1513 }</pre><p>If you have an InstanceMirror and you want to get the object |
| 1520 that it reflects, use <code class="literal">reflectee</code>. | 1514 that it reflects, use <code class="literal">reflectee</code>. This works
only if |
| 1521 This works only if the current isolate knows about the type | 1515 the current isolate knows about the type of the reflectee. Remember, |
| 1522 of the reflectee. Remember, mirrors work across isolates, and one | 1516 mirrors work across isolates, and one isolate might contain a class |
| 1523 isolate might contain a class that is not in the current | 1517 that is not in the current isolate.</p><pre class="screen"><em><span cla
ss="remark">lang-dart |
| 1524 isolate.</p><pre class="screen"><em><span class="remark">lang-dart | |
| 1525 </span></em> var person = mirror.reflectee; | 1518 </span></em> var person = mirror.reflectee; |
| 1526 assert(identical(p, person));</pre><p>Once you have an InstanceMirror, you can
invoke methods and | 1519 assert(identical(p, person));</pre><p>Once you have an InstanceMirror, you can
invoke methods and call |
| 1527 call getters and setters. | 1520 getters and setters. For a full list of methods, consult the <a class="u
link" href="http://api.dartlang.org/dart_mirrors/InstanceMirror.html" target="_t
op">API |
| 1528 For a full list of methods, | 1521 docs for InstanceMirror</a>.</p></div></div><div class="sect2"><div clas
s="titlepage"><div><div><h3 class="title"><a name="ch03-mirrors-invocation"></a>
Invocation</h3></div></div></div><p>{PENDING: To be written}</p><div class="sect
3"><div class="titlepage"><div><div><h4 class="title"><a name="idp17920016"></a>
get and set fields</h4></div></div></div></div><div class="sect3"><div class="ti
tlepage"><div><div><h4 class="title"><a name="idp17920784"></a>invoke</h4></div>
</div></div></div><div class="sect3"><div class="titlepage"><div><div><h4 class=
"title"><a name="idp17921552"></a>delegate</h4></div></div></div></div></div></d
iv><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="
clear: both"><a name="ch03-summary"></a>Summary</h2></div></div></div><p>This ch
apter introduced you to the most commonly used functionality |
| 1529 consult the <a class="ulink" href="http://api.dartlang.org/dart_mirror
s/InstanceMirror.html" target="_top">API docs | |
| 1530 for InstanceMirror</a>.</p></div></div><div class="sect2"><div class="
titlepage"><div><div><h3 class="title"><a name="ch03-mirrors-invocation"></a>Inv
ocation</h3></div></div></div><p> | |
| 1531 {To be written} | |
| 1532 </p><div class="sect3"><div class="titlepage"><div><div><h4 class="title">
<a name="idp17910288"></a>get and set fields</h4></div></div></div></div><div cl
ass="sect3"><div class="titlepage"><div><div><h4 class="title"><a name="idp17911
056"></a>invoke</h4></div></div></div></div><div class="sect3"><div class="title
page"><div><div><h4 class="title"><a name="idp17911824"></a>delegate</h4></div><
/div></div></div></div></div><div class="sect1"><div class="titlepage"><div><div
><h2 class="title" style="clear: both"><a name="ch03-summary"></a>Summary</h2></
div></div></div><p>This chapter introduced you to the most commonly used functio
nality | |
| 1533 in many of Dart’s built-in libraries. It didn’t cover all the built-in | 1522 in many of Dart’s built-in libraries. It didn’t cover all the built-in |
| 1534 libraries, however. Other libraries you might want to look into include | 1523 libraries, however. Other libraries you might want to look into include |
| 1535 <a class="ulink" href="http://api.dartlang.org/dart_collection.html" target=
"_top">dart:collection,</a> | 1524 <a class="ulink" href="http://api.dartlang.org/dart_collection.html" target=
"_top">dart:collection,</a> |
| 1536 <a class="ulink" href="http://api.dartlang.org/dart_convert.html" target="_t
op">dart:convert,</a> | 1525 <a class="ulink" href="http://api.dartlang.org/dart_convert.html" target="_t
op">dart:convert,</a> |
| 1537 <a class="ulink" href="http://api.dartlang.org/logging.html" target="_top">l
ogging,</a> and | 1526 <a class="ulink" href="http://api.dartlang.org/logging.html" target="_top">l
ogging,</a> and |
| 1538 <a class="ulink" href="http://api.dartlang.org/unittest.html" target="_top">
<span class="keep-together">unittest</span>.</a> You can use the pub tool, | 1527 <a class="ulink" href="http://api.dartlang.org/unittest.html" target="_top">
<span class="keep-together">unittest</span>.</a> You can use the pub tool, |
| 1539 discussed in the next chapter, to install additional Dart <span class="keep-
together">libraries</span>.</p></div><div class="footnotes"><br><hr style="width
:100; text-align:left;margin-left: 0"><div id="ftn.ch03-isolate-footnote-1" clas
s="footnote"><p><a href="#ch03-isolate-footnote-1" class="para"><sup class="para
">[2] </sup></a>The dart2js compiler and the Dart VM do not yet support | 1528 discussed in the next chapter, to install additional Dart <span class="keep-
together">libraries</span>.</p></div><div class="footnotes"><br><hr style="width
:100; text-align:left;margin-left: 0"><div id="ftn.ch03-isolate-footnote-1" clas
s="footnote"><p><a href="#ch03-isolate-footnote-1" class="para"><sup class="para
">[2] </sup></a>The dart2js compiler and the Dart VM do not yet support |
| 1540 static methods as isolate entry points. For details, see <a class="u
link" href="http://dartbug.com/3011" target="_top">http://dartbug.com/3011</a>.<
/p></div><div id="ftn.ch03-isolate-footnote-2" class="footnote"><p><a href="#ch0
3-isolate-footnote-2" class="para"><sup class="para">[3] </sup></a>Support for s
ending an arbitrary object to an isolate is not | 1529 static methods as isolate entry points. For details, see <a class="u
link" href="http://dartbug.com/3011" target="_top">http://dartbug.com/3011</a>.<
/p></div><div id="ftn.ch03-isolate-footnote-2" class="footnote"><p><a href="#ch0
3-isolate-footnote-2" class="para"><sup class="para">[3] </sup></a>Support for s
ending an arbitrary object to an isolate is not |
| 1541 yet available when compiling to JavaScript.</p></div></div></div><di
v class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td
width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td wid
th="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" hr
ef="ch04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C
hapter 2. A Tour of the Dart Language </td><td width="20%" align="center"><a acc
esskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="
top"> Chapter 4. Tools</td></tr></table></div> | 1530 yet available when compiling to JavaScript.</p></div></div></div><di
v class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td
width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td wid
th="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" hr
ef="ch04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C
hapter 2. A Tour of the Dart Language </td><td width="20%" align="center"><a acc
esskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="
top"> Chapter 4. Tools</td></tr></table></div> |
| OLD | NEW |