OLD | NEW |
---|---|
1 <?xml version="1.0" encoding="UTF-8"?> | 1 <?xml version="1.0" encoding="UTF-8"?> |
2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" | 2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> | 3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
4 <chapter id="ch03"> | 4 <chapter id="ch03"> |
5 <?dbhtml stop-chunking?> | 5 <?dbhtml stop-chunking?> |
6 | 6 |
7 <title>A Tour of the Dart Libraries</title> | 7 <title>A Tour of the Dart Libraries</title> |
8 | 8 |
9 <para>This chapter shows you how to use the major features in Dart’s | 9 <para>This chapter shows you how to use the major features in Dart’s |
10 libraries. It’s just an overview, and by no means comprehensive. Whenever | 10 libraries. It’s just an overview, and by no means comprehensive. Whenever |
(...skipping 1221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1232 assert(double.parse(sinOf30degrees.toStringAsPrecision(2)) == 0.5);</screen> | 1232 assert(double.parse(sinOf30degrees.toStringAsPrecision(2)) == 0.5);</screen> |
1233 | 1233 |
1234 <note> | 1234 <note> |
1235 <para>These functions use radians, not degrees!</para> | 1235 <para>These functions use radians, not degrees!</para> |
1236 </note> | 1236 </note> |
1237 </sect2> | 1237 </sect2> |
1238 | 1238 |
1239 <sect2 id="ch03-maximum-and-minimum"> | 1239 <sect2 id="ch03-maximum-and-minimum"> |
1240 <title>Maximum and Minimum</title> | 1240 <title>Maximum and Minimum</title> |
1241 | 1241 |
1242 <para>The Math library provides optimized <literal>max()</literal> and | 1242 <para>The Math library provides optimized <literal>max()</literal> and |
Emily Fortuna
2013/12/05 21:55:11
what do you mean by "optimized" methods here?
Kathy Walrath
2013/12/13 00:48:20
Done.
| |
1243 <literal>min()</literal> methods:</para> | 1243 <literal>min()</literal> methods:</para> |
1244 | 1244 |
1245 <screen format="linespecific"><remark>lang-dart | 1245 <screen format="linespecific"><remark>lang-dart |
1246 </remark><remark>ch03/math-tests.dart | 1246 </remark><remark>ch03/math-tests.dart |
1247 </remark>assert(math.max(1, 1000) == 1000); | 1247 </remark>assert(math.max(1, 1000) == 1000); |
1248 assert(math.min(1, -1000) == -1000);</screen> | 1248 assert(math.min(1, -1000) == -1000);</screen> |
1249 </sect2> | 1249 </sect2> |
1250 | 1250 |
1251 <sect2 id="ch03-math-constants"> | 1251 <sect2 id="ch03-math-constants"> |
1252 <title>Math Constants</title> | 1252 <title>Math Constants</title> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1308 <emphasis>Document Object Model</emphasis>, which describes the hierarchy | 1308 <emphasis>Document Object Model</emphasis>, which describes the hierarchy |
1309 of an HTML page.</para> | 1309 of an HTML page.</para> |
1310 | 1310 |
1311 <para>Other common uses of dart:html are manipulating styles | 1311 <para>Other common uses of dart:html are manipulating styles |
1312 (<emphasis>CSS</emphasis>), getting data using HTTP requests, and | 1312 (<emphasis>CSS</emphasis>), getting data using HTTP requests, and |
1313 exchanging data using <link linkend="ch03-websockets">WebSockets</link>. | 1313 exchanging data using <link linkend="ch03-websockets">WebSockets</link>. |
1314 HTML5 (and dart:html) has many additional APIs that this section doesn’t | 1314 HTML5 (and dart:html) has many additional APIs that this section doesn’t |
1315 cover. Only web apps can use dart:html, not command-line apps.</para> | 1315 cover. Only web apps can use dart:html, not command-line apps.</para> |
1316 | 1316 |
1317 <note> | 1317 <note> |
1318 <para>For scalable, higher level approaches to web app UIs, see <ulink | 1318 <para>For scalable, higher level approaches to web app UIs, see <ulink |
Emily Fortuna
2013/12/05 21:55:11
might consider removing "scalable" ... that seems
Kathy Walrath
2013/12/13 00:48:20
Done.
| |
1319 url="http://www.dartlang.org/polymer-dart/">Polymer.dart</ulink> and | 1319 url="http://www.dartlang.org/polymer-dart/">Polymer.dart</ulink> and |
1320 <ulink | 1320 <ulink |
1321 url="http://pub.dartlang.org/packages/angular">AngularDart</ulink>.</para> | 1321 url="http://pub.dartlang.org/packages/angular">AngularDart</ulink>.</para> |
1322 </note> | 1322 </note> |
1323 | 1323 |
1324 <para>To use the HTML library in your web app, import dart:html:</para> | 1324 <para>To use the HTML library in your web app, import dart:html:</para> |
1325 | 1325 |
1326 <programlisting format="linespecific"><remark>lang-dart | 1326 <programlisting format="linespecific"><remark>lang-dart |
1327 </remark><remark>ch03_html/ch03_html.dart | 1327 </remark><remark>ch03_html/ch03_html.dart |
1328 </remark>import 'dart:html';</programlisting> | 1328 </remark>import 'dart:html';</programlisting> |
1329 | 1329 |
1330 <note> | 1330 <note> |
1331 <para>Parts of the dart:html library are experimental, as noted in the | 1331 <para>Parts of the dart:html library are experimental, as noted in the |
1332 API documentation.</para> | 1332 API documentation.</para> |
1333 </note> | 1333 </note> |
1334 | 1334 |
1335 <sect2 id="ch03-manipulating-the-dom"> | 1335 <sect2 id="ch03-manipulating-the-dom"> |
1336 <title>Manipulating the DOM</title> | 1336 <title>Manipulating the DOM</title> |
1337 | 1337 |
1338 <para>To use the DOM, you need to know about | 1338 <para>To use the DOM, you need to know about |
1339 <emphasis>windows</emphasis>, <emphasis>documents</emphasis>, | 1339 <emphasis>windows</emphasis>, <emphasis>documents</emphasis>, |
1340 <emphasis>elements</emphasis>, and <emphasis>nodes</emphasis>.</para> | 1340 <emphasis>elements</emphasis>, and <emphasis>nodes</emphasis>.</para> |
1341 | 1341 |
1342 <para>A <ulink | 1342 <para>A <ulink |
1343 url="http://api.dartlang.org/html/Window.html">Window</ulink> object | 1343 url="http://api.dartlang.org/html/Window.html">Window</ulink> object |
1344 represents the actual window of the web browser. Each Window has a | 1344 represents the actual window of the web browser. Each Window has a |
1345 <literal moreinfo="none">document</literal> property (a Document | 1345 <literal moreinfo="none">document</literal> property (a Document |
Emily Fortuna
2013/12/05 21:55:11
Consider just saying "Document object" instead of
Kathy Walrath
2013/12/13 00:48:20
Done.
| |
1346 object), which points to the document currently loaded. The Window | 1346 object), which points to the document currently loaded. The Window |
1347 object also has accessors to various APIs such as IndexedDB (for storing | 1347 object also has accessors to various APIs such as IndexedDB (for storing |
1348 data), <literal>requestAnimationFrame()</literal> (for animations), and | 1348 data), <literal>requestAnimationFrame()</literal> (for animations), and |
Emily Fortuna
2013/12/05 21:55:11
actually the current Dart idiom is to use "animati
Kathy Walrath
2013/12/13 00:48:20
I switched to not use the () and code font, since
| |
1349 more. In tabbed browsers, each tab has its own Window object.</para> | 1349 more. In tabbed browsers, each tab has its own Window object.</para> |
1350 | 1350 |
1351 <para>With the <ulink | 1351 <para>With the <ulink |
1352 url="http://api.dartlang.org/html/Document.html">Document</ulink> | 1352 url="http://api.dartlang.org/html/Document.html">Document</ulink> |
1353 object, you can create and manipulate <ulink | 1353 object, you can create and manipulate <ulink |
1354 url="http://api.dartlang.org/html/Element.html">Elements</ulink> within | 1354 url="http://api.dartlang.org/html/Element.html">Elements</ulink> within |
1355 the document. Note that the document itself is an element and can be | 1355 the document. Note that the document itself is an element and can be |
1356 manipulated.</para> | 1356 manipulated.</para> |
1357 | 1357 |
1358 <para>The DOM models a tree of <ulink | 1358 <para>The DOM models a tree of <ulink |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1429 <para>Often you need to set properties on multiple elements. For | 1429 <para>Often you need to set properties on multiple elements. For |
1430 example, the following code sets the <literal | 1430 example, the following code sets the <literal |
1431 moreinfo="none">hidden</literal> property of all elements that have a | 1431 moreinfo="none">hidden</literal> property of all elements that have a |
1432 class of “mac”, “win”, or “linux”. Setting the <literal | 1432 class of “mac”, “win”, or “linux”. Setting the <literal |
1433 moreinfo="none">hidden</literal> property to true has the same effect | 1433 moreinfo="none">hidden</literal> property to true has the same effect |
1434 as adding <literal moreinfo="none">display:none</literal> to <phrase | 1434 as adding <literal moreinfo="none">display:none</literal> to <phrase |
1435 role="keep-together">the CSS</phrase>.</para> | 1435 role="keep-together">the CSS</phrase>.</para> |
1436 | 1436 |
1437 <screen format="linespecific"><remark>lang-dart | 1437 <screen format="linespecific"><remark>lang-dart |
1438 </remark><remark>ch03_html/ch03_html.dart | 1438 </remark><remark>ch03_html/ch03_html.dart |
1439 </remark><!-- In HTML: --> | 1439 </remark><!-- In HTML: --> |
Emily Fortuna
2013/12/05 21:55:11
consider formatting the html and the dart code int
| |
1440 <p> | 1440 <p> |
1441 <span class="os linux">Words for Linux</span> | 1441 <span class="os linux">Words for Linux</span> |
1442 <span class="os mac">Words for Mac</span> | 1442 <span class="os mac">Words for Mac</span> |
1443 <span class="os win">Words for Windows</span> | 1443 <span class="os win">Words for Windows</span> |
1444 </p> | 1444 </p> |
1445 <remark> | 1445 <remark> |
1446 </remark>// In Dart: | 1446 </remark>// In Dart: |
1447 final osList = ['mac', 'win', 'linux']; | 1447 final osList = ['mac', 'win', 'linux']; |
1448 | 1448 |
1449 var userOs = 'linux'; // In real code you'd programmatically determine this. | 1449 var userOs = 'linux'; // In real code you'd programmatically determine this. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1481 </remark>var elem = new ParagraphElement(); | 1481 </remark>var elem = new ParagraphElement(); |
1482 elem.text = 'Creating is easy!';</screen> | 1482 elem.text = 'Creating is easy!';</screen> |
1483 | 1483 |
1484 <para>You can also create an element by parsing HTML text. Any child | 1484 <para>You can also create an element by parsing HTML text. Any child |
1485 elements are also parsed and created.</para> | 1485 elements are also parsed and created.</para> |
1486 | 1486 |
1487 <screen format="linespecific"><remark>lang-dart | 1487 <screen format="linespecific"><remark>lang-dart |
1488 </remark><remark>ch03_html/ch03_html.dart | 1488 </remark><remark>ch03_html/ch03_html.dart |
1489 </remark>var elem2 = new Element.html('<p>Creating <em>is</em> easy!</p>');</screen> | 1489 </remark>var elem2 = new Element.html('<p>Creating <em>is</em> easy!</p>');</screen> |
1490 | 1490 |
1491 <para>Note that elem2 is a ParagraphElement in the above | 1491 <para>Note that elem2 is a ParagraphElement in the above |
Emily Fortuna
2013/12/05 21:55:11
"Note that elem2 is also a ParagraphElement object
Emily Fortuna
2013/12/05 21:55:11
"Note that elem2 is also a ParagraphElement object
| |
1492 example.</para> | 1492 example.</para> |
1493 | 1493 |
1494 <para>Attach the newly created element to the document by assigning a | 1494 <para>Attach the newly created element to the document by assigning a |
1495 parent to the element. You can add an element to any existing | 1495 parent to the element. You can add an element to any existing |
1496 element’s children. In the following example, <literal | 1496 element’s children. In the following example, <literal |
1497 moreinfo="none">body</literal> is an element, and its child elements | 1497 moreinfo="none">body</literal> is an element, and its child elements |
1498 are accessible (as a List<Element>) from the <literal | 1498 are accessible (as a List<Element>) from the <literal |
1499 moreinfo="none">children</literal> property.</para> | 1499 moreinfo="none">children</literal> property.</para> |
1500 | 1500 |
1501 <screen format="linespecific"><remark>lang-dart | 1501 <screen format="linespecific"><remark>lang-dart |
1502 </remark><remark>ch03_html/ch03_html.dart | 1502 </remark><remark>ch03_html/ch03_html.dart |
1503 </remark>document.body.children.add(elem2);</screen> | 1503 </remark>document.body.children.add(elem2);</screen> |
1504 </sect3> | 1504 </sect3> |
1505 | 1505 |
1506 <sect3 id="ch03-adding-replacing-and-removing-nodes"> | 1506 <sect3 id="ch03-adding-replacing-and-removing-nodes"> |
1507 <title>Adding, replacing, and removing nodes</title> | 1507 <title>Adding, replacing, and removing nodes</title> |
1508 | 1508 |
1509 <para>Recall that elements are just a kind of node. You can find all | 1509 <para>Recall that elements are just a kind of node. You can find all |
1510 the children of a node using the <literal | 1510 the children of a node using the <literal |
1511 moreinfo="none">nodes</literal> property of Node, which returns a | 1511 moreinfo="none">nodes</literal> property of Node, which returns a |
1512 List<Node>. Once you have this list, you can use the usual List | 1512 List<Node>. Once you have this list, you can use the usual List |
Emily Fortuna
2013/12/05 21:55:11
maybe add "as opposed to 'children' which just ret
Kathy Walrath
2013/12/13 00:48:20
Done.
| |
1513 methods and operators to manipulate the children of the node.</para> | 1513 methods and operators to manipulate the children of the node.</para> |
1514 | 1514 |
1515 <para>To add a node as the last child of its parent, use the List | 1515 <para>To add a node as the last child of its parent, use the List |
1516 <literal>add()</literal> method:</para> | 1516 <literal>add()</literal> method:</para> |
1517 | 1517 |
1518 <screen format="linespecific"><remark>lang-dart | 1518 <screen format="linespecific"><remark>lang-dart |
1519 </remark><remark>ch03_html/ch03_html.dart | 1519 </remark><remark>ch03_html/ch03_html.dart |
1520 </remark>// Find the parent by ID, and add elem as its last child. | 1520 </remark>// Find the parent by ID, and add elem as its last child. |
1521 querySelector('#inputs').nodes.add(elem);</screen> | 1521 querySelector('#inputs').nodes.add(elem);</screen> |
1522 | 1522 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1564 </remark>var message = new DivElement(); | 1564 </remark>var message = new DivElement(); |
1565 message.id = 'message2'; | 1565 message.id = 'message2'; |
1566 message.text = 'Please subscribe to the Dart mailing list.';</screen> | 1566 message.text = 'Please subscribe to the Dart mailing list.';</screen> |
1567 | 1567 |
1568 <para>You can reduce the redundant text in this example by using | 1568 <para>You can reduce the redundant text in this example by using |
1569 method cascades:</para> | 1569 method cascades:</para> |
1570 | 1570 |
1571 <screen format="linespecific"><remark>lang-dart | 1571 <screen format="linespecific"><remark>lang-dart |
1572 </remark><remark>ch03_html/ch03_html.dart | 1572 </remark><remark>ch03_html/ch03_html.dart |
1573 </remark>var message = new DivElement() | 1573 </remark>var message = new DivElement() |
1574 ..id = 'message2' | 1574 ..id = 'message2' |
Emily Fortuna
2013/12/05 21:55:11
stylistic note: at google, when the line is a meth
Kathy Walrath
2013/12/13 00:48:20
I had to look this one up. According to https://co
| |
1575 ..text = 'Please subscribe to the Dart mailing list.';</screen> | 1575 ..text = 'Please subscribe to the Dart mailing list.';</screen> |
1576 | 1576 |
1577 <para>While using IDs and classes to associate an element with a set | 1577 <para>While using IDs and classes to associate an element with a set |
1578 of styles is best practice, sometimes you want to attach a specific | 1578 of styles is best practice, sometimes you want to attach a specific |
1579 style directly to the element:</para> | 1579 style directly to the element:</para> |
1580 | 1580 |
1581 <screen format="linespecific"><remark>lang-dart | 1581 <screen format="linespecific"><remark>lang-dart |
1582 </remark><remark>ch03_html/ch03_html.dart | 1582 </remark><remark>ch03_html/ch03_html.dart |
1583 </remark>message.style | 1583 </remark>message.style |
1584 ..fontWeight = 'bold' | 1584 ..fontWeight = 'bold' |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1750 print('License: $license'); | 1750 print('License: $license'); |
1751 } catch(e) { | 1751 } catch(e) { |
1752 print('$xmlUri doesn\'t have correct XML formatting.'); | 1752 print('$xmlUri doesn\'t have correct XML formatting.'); |
1753 } | 1753 } |
1754 } | 1754 } |
1755 ...</screen> | 1755 ...</screen> |
1756 | 1756 |
1757 <para>You can also use the full API to handle more interesting cases. | 1757 <para>You can also use the full API to handle more interesting cases. |
1758 For example, you can set arbitrary headers.</para> | 1758 For example, you can set arbitrary headers.</para> |
1759 | 1759 |
1760 <para>The general flow for using the full API of HttpRequest is as | 1760 <para>The general flow for using the full API of HttpRequest is as |
Emily Fortuna
2013/12/05 21:55:11
maybe just condense this list into a sentence. Giv
| |
1761 follows:</para> | 1761 follows:</para> |
1762 | 1762 |
1763 <orderedlist continuation="restarts" inheritnum="ignore" | 1763 <orderedlist continuation="restarts" inheritnum="ignore" |
1764 numeration="arabic"> | 1764 numeration="arabic"> |
1765 <listitem> | 1765 <listitem> |
1766 <para>Create the HttpRequest object.</para> | 1766 <para>Create the HttpRequest object.</para> |
1767 </listitem> | 1767 </listitem> |
1768 | 1768 |
1769 <listitem> | 1769 <listitem> |
1770 <para>Open the URL with either <literal | 1770 <para>Open the URL with either <literal |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1951 url="http://www.dartlang.org/samples/">Dart Code Samples.</ulink> Dart | 1951 url="http://www.dartlang.org/samples/">Dart Code Samples.</ulink> Dart |
1952 has additional libraries for more specialized web APIs, such as <ulink | 1952 has additional libraries for more specialized web APIs, such as <ulink |
1953 url="http://api.dartlang.org/dart_web_audio.html">web audio,</ulink> | 1953 url="http://api.dartlang.org/dart_web_audio.html">web audio,</ulink> |
1954 <ulink | 1954 <ulink |
1955 url="http://api.dartlang.org/dart_indexed_db.html">IndexedDB</ulink>, | 1955 url="http://api.dartlang.org/dart_indexed_db.html">IndexedDB</ulink>, |
1956 and <ulink | 1956 and <ulink |
1957 url="http://api.dartlang.org/dart_web_gl.html">WebGL</ulink>.</para> | 1957 url="http://api.dartlang.org/dart_web_gl.html">WebGL</ulink>.</para> |
1958 </sect2> | 1958 </sect2> |
1959 </sect1> | 1959 </sect1> |
1960 | 1960 |
1961 <sect1 id="ch03-dartisolate---concurrency-with-isolates"> | |
1962 <title>dart:isolate - Concurrency with Isolates</title> | |
1963 | |
1964 <para>Dart has no shared-memory threads. Instead, all Dart code runs in | |
1965 isolates, which communicate via message passing. Messages are copied | |
1966 before they are received, ensuring that no two isolates can manipulate the | |
1967 same object instance. Because state is managed by individual isolates, no | |
1968 locks or mutexes are needed, greatly simplifying concurrent | |
1969 programming.</para> | |
1970 | |
1971 <note> | |
1972 <para>The isolate API changed recently. We'll update this section to | |
1973 reflect the new API. <remark> {PENDING: update!!!} </remark></para> | |
1974 </note> | |
1975 | |
1976 <remark><p /></remark> | |
1977 | |
1978 <sect2 id="ch03-isolate-concepts"> | |
1979 <title>Isolate Concepts</title> | |
1980 | |
1981 <para>To use isolates, you should understand the following | |
1982 concepts:</para> | |
1983 | |
1984 <itemizedlist> | |
1985 <listitem> | |
1986 <para>No two isolates ever share the same thread at the same time. | |
1987 Within an isolate, callbacks execute one at a time, making the code | |
1988 more predictable.</para> | |
1989 </listitem> | |
1990 | |
1991 <listitem> | |
1992 <para>All values in memory, including globals, are available only to | |
1993 their isolate. No isolate can see or manipulate values owned by | |
1994 another isolate.</para> | |
1995 </listitem> | |
1996 | |
1997 <listitem> | |
1998 <para>The only way isolates can communicate with each other is by | |
1999 passing messages.</para> | |
2000 </listitem> | |
2001 | |
2002 <listitem> | |
2003 <para>Isolates send messages using SendPorts, and receive them using | |
2004 ReceivePorts.</para> | |
2005 </listitem> | |
2006 | |
2007 <listitem> | |
2008 <para>The content of a message can be any of the following:</para> | |
2009 | |
2010 <itemizedlist> | |
2011 <listitem> | |
2012 <para>A primitive value (null, num, bool, double, String)</para> | |
2013 </listitem> | |
2014 | |
2015 <listitem> | |
2016 <para>An instance of SendPort</para> | |
2017 </listitem> | |
2018 | |
2019 <listitem> | |
2020 <para>A list or map whose elements are any of the above, | |
2021 including other lists and maps</para> | |
2022 </listitem> | |
2023 </itemizedlist> | |
2024 </listitem> | |
2025 </itemizedlist> | |
2026 | |
2027 <remark>Deleted this additional content type: "In special | |
2028 circumstances(ch03-sending-any-type-of-object), an object of any type." | |
2029 Also deleted the following bulleted items: (1) Each isolate has a | |
2030 ReceivePort, which is available as the <literal | |
2031 moreinfo="none">port</literal> variable. Because all Dart code runs | |
2032 inside an isolate, even <literal>main()</literal> has access to a port | |
2033 object. (2) When a web application is compiled to JavaScript, its | |
2034 isolates can be implemented as Web workers. When running in Dartium, | |
2035 isolates run in the VM. (3) In the standalone VM, the | |
2036 <literal>main()</literal> function runs in the first isolate (also known | |
2037 as the <emphasis>root isolate</emphasis>). When the root isolate | |
2038 terminates, it terminates the whole VM, regardless of whether other | |
2039 isolates are still running. For more information, see | |
2040 ch03-keeping-the-root-isolate-alive.</remark> | |
2041 | |
2042 <remark>Removed section on Using Isolates(ch03-using-isolates) | |
2043 (subsections: [Spawning isolates](ch03-spawning-isolates), [Sending | |
2044 messages](ch03-sending-messages), [Sending any type of | |
2045 object](ch03-sending-any-type-of-object), [Receiving | |
2046 messages](ch03-receiving-messages) [Receivingn | |
2047 replies](ch03-receiving-replies) [Keeping the root isolate | |
2048 alive](ch03-keeping-the-root-isolate-alive)).</remark> | |
2049 </sect2> | |
2050 | |
2051 <sect2 id="ch03-more-information-11"> | |
2052 <title>More Information</title> | |
2053 | |
2054 <para>See the API docs for the <ulink | |
2055 url="http://api.dartlang.org/dart_isolate.html">dart:isolate | |
2056 library,</ulink> as well as for <ulink | |
2057 url="http://api.dartlang.org/dart_isolate/SendPort.html">SendPort</ulink> | |
2058 and <ulink role="keep-together" security="" | |
2059 url="http://api.dartlang.org/dart_isolate/ReceivePort.html">ReceivePort.</ ulink></para> | |
2060 </sect2> | |
2061 </sect1> | |
2062 | |
2063 <sect1 id="ch03-dartio---file-and-socket-io-for-command-line-apps"> | 1961 <sect1 id="ch03-dartio---file-and-socket-io-for-command-line-apps"> |
2064 <title>dart:io - I/O for Command-Line Apps</title> | 1962 <title>dart:io - I/O for Command-Line Apps</title> |
2065 | 1963 |
2066 <para>The <ulink url="http://api.dartlang.org/io.html">dart:io | 1964 <para>The <ulink url="http://api.dartlang.org/io.html">dart:io |
2067 library</ulink> provides APIs to deal with files, directories, processes, | 1965 library</ulink> provides APIs to deal with files, directories, processes, |
2068 sockets, and HTTP connections. Only command-line apps can use dart:io—not | 1966 sockets, and HTTP connections. Only command-line apps can use dart:io—not |
2069 web apps.</para> | 1967 web apps.</para> |
2070 | 1968 |
2071 <para>In general, the dart:io library implements and promotes an | 1969 <para>In general, the dart:io library implements and promotes an |
2072 asynchronous API. Synchronous methods can easily block the event loop, | 1970 asynchronous API. Synchronous methods can easily block the event loop, |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2549 | 2447 |
2550 <para>The dart:mirrors library provides basic reflection abilities to | 2448 <para>The dart:mirrors library provides basic reflection abilities to |
2551 Dart. Use mirrors to query the structure of your program and to | 2449 Dart. Use mirrors to query the structure of your program and to |
2552 dynamically invoke functions or methods at runtime. Mirrors can also work | 2450 dynamically invoke functions or methods at runtime. Mirrors can also work |
2553 on static source code. For example, the dartdoc tool (which generates HTML | 2451 on static source code. For example, the dartdoc tool (which generates HTML |
2554 docs from Dart source code) uses source-code mirrors.</para> | 2452 docs from Dart source code) uses source-code mirrors.</para> |
2555 | 2453 |
2556 <para>The dart:mirrors library works in both web apps and command-line | 2454 <para>The dart:mirrors library works in both web apps and command-line |
2557 apps. To use it, import dart:mirrors.</para> | 2455 apps. To use it, import dart:mirrors.</para> |
2558 | 2456 |
2559 <note> | 2457 <warning> |
2560 <para>The dart:mirrors library is still under development. Its API might | 2458 <para>Using dart:mirrors can cause dart2js to generate very large |
2561 change slightly as a result of user feedback.</para> | 2459 JavaScript files.</para> |
2562 </note> | 2460 |
2461 <para>The current workaround is to add a <literal>@MirrorsUsed</literal> | |
2462 annotation before the import of dart:mirrors. For details, see the | |
2463 <ulink | |
2464 url="http://api.dartlang.org/dart_mirrors/MirrorsUsed.html">MirrorsUsed</u link> | |
2465 API documentation. This workaround might change, as the dart:mirrors | |
2466 library is still under development.</para> | |
2467 </warning> | |
2563 | 2468 |
2564 <sect2 id="ch03-mirrors-symbols"> | 2469 <sect2 id="ch03-mirrors-symbols"> |
2565 <title>Symbols</title> | 2470 <title>Symbols</title> |
2566 | 2471 |
2567 <para>The mirror system represents the names of Dart declarations | 2472 <para>The mirror system represents the names of Dart declarations |
2568 (classes, fields, and so on) by instances of the class <ulink | 2473 (classes, fields, and so on) by instances of the class <ulink |
2569 url="http://api.dartlang.org/dart_core/Symbol.html">Symbol</ulink>. | 2474 url="http://api.dartlang.org/dart_core/Symbol.html">Symbol</ulink>. |
2570 Symbols help compilers like dart2js produce efficient output, even when | 2475 Symbols help compilers like dart2js produce efficient output, even when |
2571 minified.</para> | 2476 minified.</para> |
2572 | 2477 |
(...skipping 26 matching lines...) Expand all Loading... | |
2599 assert('MyClass' == MirrorSystem.getName(className)); | 2504 assert('MyClass' == MirrorSystem.getName(className)); |
2600 </screen> | 2505 </screen> |
2601 </sect2> | 2506 </sect2> |
2602 | 2507 |
2603 <sect2 id="ch03-mirrors-introspection"> | 2508 <sect2 id="ch03-mirrors-introspection"> |
2604 <title>Introspection</title> | 2509 <title>Introspection</title> |
2605 | 2510 |
2606 <para>Use mirrors to introspect the running program's structure. You can | 2511 <para>Use mirrors to introspect the running program's structure. You can |
2607 inspect classes, libraries, instances, and more.</para> | 2512 inspect classes, libraries, instances, and more.</para> |
2608 | 2513 |
2609 <note> | 2514 <para>The examples in this section use the following Person |
2610 <para>The dart:mirrors library changed recently. We'll update this | 2515 class:</para> |
2611 section soon to reflect the new API. <remark> {PENDING: update!!!} | 2516 |
2612 Removed the following: The examples in this section use the following | 2517 <screen format="linespecific"><remark>lang-dart |
2613 Person class: class Person { String firstName; String lastName; int | 2518 ch03/mirrors.dart |
2614 age; Person(this.firstName, this.lastName, this.age); String get | 2519 </remark>class Person { |
2615 fullName => '$firstName $lastName'; void greet(String other) { | 2520 String firstName; |
2616 print('Hello there, $other!'); } } To begin, you need to | 2521 String lastName; |
2617 <emphasis>reflect</emphasis> on a class or object to get its | 2522 int age; |
2618 <emphasis>mirror</emphasis>. SECT3: Class Mirrors. Reflect on a Type | 2523 |
2619 to get its ClassMirror. {e.g.} You can also call | 2524 Person(this.firstName, this.lastName, this.age); |
2620 <literal>runtimeType</literal> to get a Type from an instance. {e.g.} | 2525 |
2621 Once you have a ClassMirror, you can get a class's constructors, | 2526 String get fullName => '$firstName $lastName'; |
2527 | |
2528 void greet(String other) { | |
2529 print('Hello there, $other!'); | |
2530 } | |
2531 }</screen> | |
2532 | |
2533 <para>To begin, you need to <emphasis>reflect</emphasis> on a class or | |
2534 object to get its <emphasis>mirror</emphasis>.</para> | |
2535 | |
2536 <sect3 id="ch03-mirrors-class-inspect"> | |
2537 <title>Class Mirrors</title> | |
2538 | |
2539 <para>Reflect on a Type to get its ClassMirror.</para> | |
2540 | |
2541 <screen format="linespecific"><remark>lang-dart | |
2542 ch03/mirrors.dart | |
2543 </remark>ClassMirror mirror = reflectClass(Person); | |
2544 | |
2545 assert('Person' == MirrorSystem.getName(mirror.simpleName));</screen> | |
2546 | |
2547 <para>You can also call <literal>runtimeType</literal> to get a Type | |
2548 from an instance.</para> | |
2549 | |
2550 <screen format="linespecific"><remark>lang-dart | |
2551 ch03/mirrors.dart | |
2552 </remark>var person = new Person('Bob', 'Smith', 33); | |
2553 ClassMirror mirror = reflectClass(person.runtimeType); | |
2554 | |
2555 assert('Person' == MirrorSystem.getName(mirror.simpleName));</screen> | |
2556 | |
2557 <para>Once you have a ClassMirror, you can get a class's constructors, | |
2622 fields, and more. Here is an example of listing the constructors of a | 2558 fields, and more. Here is an example of listing the constructors of a |
2623 class. {e.g.} Here is an example of listing all of the fields declared | 2559 class.</para> |
2624 by a class. {e.g.} For a full list of methods, consult the <ulink | 2560 |
2561 <screen format="linespecific"><remark>lang-dart | |
2562 ch03/mirrors.dart | |
2563 </remark>showConstructors(ClassMirror mirror) { | |
2564 var methods = mirror.declarations.values.where((m) => m is MethodMirror); | |
2565 var constructors = methods.where((m) => m.isConstructor); | |
2566 | |
2567 constructors.forEach((m) { | |
2568 print('The constructor ${m.simpleName} has ${m.parameters.length} parameters .'); | |
2569 }); | |
2570 }</screen> | |
2571 | |
2572 <para>Here is an example of listing all of the fields declared by a | |
2573 class.</para> | |
2574 | |
2575 <screen><remark>lang-dart | |
2576 ch03/mirrors.dart | |
2577 </remark>showFields(ClassMirror mirror) { | |
2578 var fields = mirror.declarations.values.where((m) => m is VariableMirror); | |
2579 | |
2580 fields.forEach((VariableMirror m) { | |
2581 var finalStatus = m.isFinal ? 'final' : 'not final'; | |
2582 var privateStatus = m.isPrivate ? 'private' : 'not private'; | |
2583 var typeAnnotation = m.type.simpleName; | |
2584 | |
2585 print('The field ${m.simpleName} is $privateStatus and $finalStatus and is a nnotated ' | |
2586 'as $typeAnnotation'); | |
2587 }); | |
2588 }</screen> | |
2589 | |
2590 <para>For a full list of methods, consult the <ulink | |
2625 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">API docs | 2591 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">API docs |
2626 for ClassMirror</ulink>. SECT3: Instance Mirrors. Reflect on an object | 2592 for ClassMirror</ulink>.</para> |
2627 to get an InstanceMirror. {e.g.} If you have an InstanceMirror and you | 2593 </sect3> |
2628 want to get the object that it reflects, use | 2594 |
2629 <literal>reflectee</literal>. This works only if the current isolate | 2595 <sect3 id="ch03-mirrors-instance-inspect"> |
2630 knows about the type of the reflectee. Remember, mirrors work across | 2596 <title>Instance Mirrors</title> |
2631 isolates, and one isolate might contain a class that is not in the | 2597 |
2632 current isolate. Once you have an InstanceMirror, you can invoke | 2598 <para>Reflect on an object to get an InstanceMirror.</para> |
2633 methods and call getters and setters. For a full list of methods, | 2599 |
2634 consult the <ulink | 2600 <screen><remark>lang-dart |
2635 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">API | 2601 ch03/mirrors.dart |
2636 docs for InstanceMirror</ulink>. SECT2: Invocation. {PENDING: To be | 2602 </remark>var p = new Person('Bob', 'Smith', 42); |
2637 written} SECT3: get and set fields. SECT3: invoke. SECT3: delegate | 2603 InstanceMirror mirror = reflect(p);</screen> |
2638 </remark></para> | 2604 |
2639 </note> | 2605 <para>If you have an InstanceMirror and you want to get the object |
2606 that it reflects, use <literal>reflectee</literal>.</para> | |
2607 | |
2608 <screen><remark>lang-dart | |
2609 ch03/mirrors.dart | |
2610 </remark>var person = mirror.reflectee; | |
2611 assert(identical(p, person));</screen> | |
2612 </sect3> | |
2613 </sect2> | |
2614 | |
2615 <sect2 id="ch03-mirrors-invocation"> | |
2616 <title>Invocation</title> | |
2617 | |
2618 <para>Once you have an InstanceMirror, you can invoke methods and call | |
2619 getters and setters. For a full list of methods, consult the <ulink | |
2620 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">API docs | |
2621 for InstanceMirror</ulink>.</para> | |
2622 | |
2623 <sect3 id="ch03-mirrors-invoke"> | |
2624 <title>Invoke Methods</title> | |
2625 | |
2626 <para>Use InstanceMirror's <literal>invoke()</literal> method to | |
2627 invoke a method on an object. The first parameter specifies the method | |
2628 to be invoked, and the second is a list of positional arguments to the | |
2629 method. An optional third parameter lets you specify named | |
2630 arguments.</para> | |
2631 | |
2632 <screen><remark>lang-dart | |
2633 ch03/mirrors.dart | |
2634 </remark>var p = new Person('Bob', 'Smith', 42); | |
2635 InstanceMirror mirror = reflect(p); | |
2636 | |
2637 mirror.invoke(#greet, ['Shailen']);</screen> | |
2638 </sect3> | |
2639 | |
2640 <sect3 id="ch03-mirrors-getset-fields"> | |
2641 <title>Get and Set Properties</title> | |
2642 | |
2643 <para>Use InstanceMirror's <literal>getField()</literal> and | |
2644 <literal>setField()</literal> methods to get and set properties of an | |
2645 object.</para> | |
2646 | |
2647 <screen><remark>lang-dart | |
2648 ch03/mirrors.dart | |
2649 </remark> | |
2650 var p = new Person('Bob', 'Smith', 42); | |
2651 InstanceMirror mirror = reflect(p); | |
2652 | |
2653 // Get the value of a property. | |
2654 var fullName = mirror.getField(#fullName).reflectee; | |
2655 assert(fullName == 'Bob Smith'); | |
2656 | |
2657 // Set the value of a property. | |
2658 mirror.setField(#firstName, 'Mary'); | |
2659 assert(p.firstName == 'Mary');</screen> | |
2660 </sect3> | |
2661 </sect2> | |
2662 | |
2663 <sect2 id="ch03-more-information-mirrors"> | |
2664 <title>More Information</title> | |
2665 | |
2666 <para>The article <ulink | |
2667 url="https://www.dartlang.org/articles/reflection-with-mirrors/">Reflectio n | |
2668 in Dart with Mirrors</ulink> has more information and examples. Also see | |
2669 the API docs for <ulink | |
2670 url="http://api.dartlang.org/dart_mirrors.html">dart:mirror,</ulink> | |
2671 especially <ulink | |
2672 url="http://api.dartlang.org/dart_mirrors/MirrorsUsed.html">MirrorsUsed</u link>, | |
2673 <ulink | |
2674 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">ClassMirror,</ ulink> | |
2675 and <ulink | |
2676 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">InstanceMir ror.</ulink></para> | |
2640 </sect2> | 2677 </sect2> |
2641 </sect1> | 2678 </sect1> |
2642 | 2679 |
2643 <sect1 id="ch03-summary"> | 2680 <sect1 id="ch03-summary"> |
2644 <title>Summary</title> | 2681 <title>Summary</title> |
2645 | 2682 |
2646 <para>This chapter introduced you to the most commonly used functionality | 2683 <para>This chapter introduced you to the most commonly used functionality |
2647 in many of Dart’s built-in libraries. It didn’t cover all the built-in | 2684 in many of Dart’s built-in libraries. It didn’t cover all the built-in |
2648 libraries, however. Others that you might want to look into include <ulink | 2685 libraries, however. Others that you might want to look into include <ulink |
2649 url="http://api.dartlang.org/dart_collection.html">dart:collection,</ulink> | 2686 url="http://api.dartlang.org/dart_collection.html">dart:collection,</ulink> |
2687 <ulink | |
2688 url="http://api.dartlang.org/dart_isolate.html">dart:isolate,</ulink> | |
2650 <ulink url="http://api.dartlang.org/dart_js.html">dart:js,</ulink> and | 2689 <ulink url="http://api.dartlang.org/dart_js.html">dart:js,</ulink> and |
2651 <ulink | 2690 <ulink |
2652 url="http://api.dartlang.org/dart_typed_data.html">dart:typed_data.</ulink> | 2691 url="http://api.dartlang.org/dart_typed_data.html">dart:typed_data.</ulink> |
2653 You can get yet more libraries by using the pub tool, discussed in the | 2692 You can get yet more libraries by using the pub tool, discussed in the |
2654 next chapter. The <ulink | 2693 next chapter. The <ulink |
2655 url="http://pub.dartlang.org/packages/args">args,</ulink> <ulink | 2694 url="http://pub.dartlang.org/packages/args">args,</ulink> <ulink |
2656 url="http://api.dartlang.org/logging.html">logging,</ulink> <ulink | 2695 url="http://api.dartlang.org/logging.html">logging,</ulink> <ulink |
2657 url="http://pub.dartlang.org/packages/polymer">polymer,</ulink> and <ulink | 2696 url="http://pub.dartlang.org/packages/polymer">polymer,</ulink> and <ulink |
2658 url="http://api.dartlang.org/unittest.html" wordsize=""><phrase | 2697 url="http://api.dartlang.org/unittest.html" wordsize=""><phrase |
2659 role="keep-together">unittest</phrase></ulink> libraries are just a | 2698 role="keep-together">unittest</phrase></ulink> libraries are just a |
2660 sampling of what you can install using pub.</para> | 2699 sampling of what you can install using pub.</para> |
2661 </sect1> | 2700 </sect1> |
2662 </chapter> | 2701 </chapter> |
OLD | NEW |