Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: ch03.xml

Issue 103483002: add back dart:mirrors content, remove dart:isolate, misc. tweaks/updates (Closed) Base URL: https://github.com/dart-lang/dart-up-and-running-book.git@master
Patch Set: move a misplaced comment Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« ch00.xml ('K') | « ch02.xml ('k') | code/ch03/mirrors.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1940 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
2457 <screen format="linespecific"><remark>lang-dart
2458 ch03/mirrors.dart
2459 </remark>import 'dart:mirrors';
2460
2461 // If the symbol name is known at compile time.
2462 const className = #MyClass;
2463
2464 // If the symbol name is dynamically determined.
2465 var userInput = askUserForNameOfFunction();
2466 var functionName = new Symbol(userInput);</screen>
2467
2559 <note> 2468 <note>
2560 <para>The dart:mirrors library is still under development. Its API might 2469 <para>The dart:mirrors library is still under development. Its API might
2561 change slightly as a result of user feedback.</para> 2470 change slightly as a result of user feedback.</para>
2562 </note> 2471 </note>
2563 2472
2564 <sect2 id="ch03-mirrors-symbols"> 2473 <sect2 id="ch03-mirrors-symbols">
2565 <title>Symbols</title> 2474 <title>Symbols</title>
2566 2475
2567 <para>The mirror system represents the names of Dart declarations 2476 <para>The mirror system represents the names of Dart declarations
2568 (classes, fields, and so on) by instances of the class <ulink 2477 (classes, fields, and so on) by instances of the class <ulink
(...skipping 30 matching lines...) Expand all
2599 assert('MyClass' == MirrorSystem.getName(className)); 2508 assert('MyClass' == MirrorSystem.getName(className));
2600 </screen> 2509 </screen>
2601 </sect2> 2510 </sect2>
2602 2511
2603 <sect2 id="ch03-mirrors-introspection"> 2512 <sect2 id="ch03-mirrors-introspection">
2604 <title>Introspection</title> 2513 <title>Introspection</title>
2605 2514
2606 <para>Use mirrors to introspect the running program's structure. You can 2515 <para>Use mirrors to introspect the running program's structure. You can
2607 inspect classes, libraries, instances, and more.</para> 2516 inspect classes, libraries, instances, and more.</para>
2608 2517
2609 <note> 2518 <para>The examples in this section use the following Person
2610 <para>The dart:mirrors library changed recently. We'll update this 2519 class:</para>
2611 section soon to reflect the new API. <remark> {PENDING: update!!!} 2520
2612 Removed the following: The examples in this section use the following 2521 <screen format="linespecific"><remark>lang-dart
2613 Person class: class Person { String firstName; String lastName; int 2522 ch03/mirrors.dart
2614 age; Person(this.firstName, this.lastName, this.age); String get 2523 </remark>class Person {
2615 fullName =&gt; '$firstName $lastName'; void greet(String other) { 2524 String firstName;
2616 print('Hello there, $other!'); } } To begin, you need to 2525 String lastName;
2617 <emphasis>reflect</emphasis> on a class or object to get its 2526 int age;
2618 <emphasis>mirror</emphasis>. SECT3: Class Mirrors. Reflect on a Type 2527
2619 to get its ClassMirror. {e.g.} You can also call 2528 Person(this.firstName, this.lastName, this.age);
2620 <literal>runtimeType</literal> to get a Type from an instance. {e.g.} 2529
2621 Once you have a ClassMirror, you can get a class's constructors, 2530 String get fullName =&gt; '$firstName $lastName';
2531
2532 void greet(String other) {
2533 print('Hello there, $other!');
2534 }
2535 }</screen>
2536
2537 <para>To begin, you need to <emphasis>reflect</emphasis> on a class or
2538 object to get its <emphasis>mirror</emphasis>.</para>
2539
2540 <sect3 id="ch03-mirrors-class-inspect">
2541 <title>Class Mirrors</title>
2542
2543 <para>Reflect on a Type to get its ClassMirror.</para>
2544
2545 <screen format="linespecific"><remark>lang-dart
2546 ch03/mirrors.dart
2547 </remark>ClassMirror mirror = reflectClass(Person);
2548
2549 assert('Person' == MirrorSystem.getName(mirror.simpleName));</screen>
2550
2551 <para>You can also call <literal>runtimeType</literal> to get a Type
2552 from an instance.</para>
2553
2554 <screen format="linespecific"><remark>lang-dart
2555 ch03/mirrors.dart
2556 </remark>var person = new Person('Bob', 'Smith', 33);
2557 ClassMirror mirror = reflectClass(person.runtimeType);
2558
2559 assert('Person' == MirrorSystem.getName(mirror.simpleName));</screen>
2560
2561 <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 2562 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 2563 class.</para>
2624 by a class. {e.g.} For a full list of methods, consult the <ulink 2564
2565 <screen format="linespecific"><remark>lang-dart
2566 ch03/mirrors.dart
2567 </remark>showConstructors(ClassMirror mirror) {
2568 var methods = mirror.declarations.values.where((m) =&gt; m is MethodMirror);
2569 var constructors = methods.where((m) =&gt; m.isConstructor);
2570
2571 constructors.forEach((m) {
2572 print('The constructor ${m.simpleName} has ${m.parameters.length} parameters .');
2573 });
2574 }</screen>
2575
2576 <para>Here is an example of listing all of the fields declared by a
2577 class.</para>
2578
2579 <screen><remark>lang-dart
2580 ch03/mirrors.dart
2581 </remark>showFields(ClassMirror mirror) {
2582 var fields = mirror.declarations.values.where((m) =&gt; m is VariableMirror);
2583
2584 fields.forEach((VariableMirror m) {
2585 var finalStatus = m.isFinal ? 'final' : 'not final';
2586 var privateStatus = m.isPrivate ? 'private' : 'not private';
2587 var typeAnnotation = m.type.simpleName;
2588
2589 print('The field ${m.simpleName} is $privateStatus and $finalStatus and is a nnotated '
2590 'as $typeAnnotation');
2591 });
2592 }</screen>
2593
2594 <para>For a full list of methods, consult the <ulink
2625 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">API docs 2595 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">API docs
2626 for ClassMirror</ulink>. SECT3: Instance Mirrors. Reflect on an object 2596 for ClassMirror</ulink>.</para>
2627 to get an InstanceMirror. {e.g.} If you have an InstanceMirror and you 2597 </sect3>
2628 want to get the object that it reflects, use 2598
2629 <literal>reflectee</literal>. This works only if the current isolate 2599 <sect3 id="ch03-mirrors-instance-inspect">
2630 knows about the type of the reflectee. Remember, mirrors work across 2600 <title>Instance Mirrors</title>
2631 isolates, and one isolate might contain a class that is not in the 2601
2632 current isolate. Once you have an InstanceMirror, you can invoke 2602 <para>Reflect on an object to get an InstanceMirror.</para>
2633 methods and call getters and setters. For a full list of methods, 2603
2634 consult the <ulink 2604 <screen><remark>lang-dart
2635 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">API 2605 ch03/mirrors.dart
2636 docs for InstanceMirror</ulink>. SECT2: Invocation. {PENDING: To be 2606 </remark>var p = new Person('Bob', 'Smith', 42);
2637 written} SECT3: get and set fields. SECT3: invoke. SECT3: delegate 2607 InstanceMirror mirror = reflect(p);</screen>
2638 </remark></para> 2608
2639 </note> 2609 <para>If you have an InstanceMirror and you want to get the object
2610 that it reflects, use <literal>reflectee</literal>.</para>
2611
2612 <screen><remark>lang-dart
2613 ch03/mirrors.dart
2614 </remark>var person = mirror.reflectee;
2615 assert(identical(p, person));</screen>
2616 </sect3>
2617 </sect2>
2618
2619 <sect2 id="ch03-mirrors-invocation">
2620 <title>Invocation</title>
2621
2622 <para>Once you have an InstanceMirror, you can invoke methods and call
2623 getters and setters. For a full list of methods, consult the <ulink
2624 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">API docs
2625 for InstanceMirror</ulink>.</para>
2626
2627 <sect3 id="ch03-mirrors-invoke">
2628 <title>Invoke Methods</title>
2629
2630 <para>Use InstanceMirror's <literal>invoke()</literal> method to
2631 invoke a method on an object. The first parameter specifies the method
2632 to be invoked, and the second is a list of positional arguments to the
2633 method. An optional third parameter lets you specify named
2634 arguments.</para>
2635
2636 <screen><remark>lang-dart
2637 ch03/mirrors.dart
2638 </remark>var p = new Person('Bob', 'Smith', 42);
2639 InstanceMirror mirror = reflect(p);
2640
2641 mirror.invoke(#greet, ['Shailen']);</screen>
2642 </sect3>
2643
2644 <sect3 id="ch03-mirrors-getset-fields">
2645 <title>Get and Set Properties</title>
2646
2647 <para>Use InstanceMirror's <literal>getField()</literal> and
2648 <literal>setField()</literal> methods to get and set properties of an
2649 object.</para>
2650
2651 <screen><remark>lang-dart
2652 ch03/mirrors.dart
2653 </remark>
2654 var p = new Person('Bob', 'Smith', 42);
2655 InstanceMirror mirror = reflect(p);
2656
2657 // Get the value of a property.
2658 var fullName = mirror.getField(#fullName).reflectee;
2659 assert(fullName == 'Bob Smith');
2660
2661 // Set the value of a property.
2662 mirror.setField(#firstName, 'Mary');
2663 assert(p.firstName == 'Mary');</screen>
2664 </sect3>
2665 </sect2>
2666
2667 <sect2 id="ch03-more-information-mirrors">
2668 <title>More Information</title>
2669
2670 <para>The article <ulink
2671 url="https://www.dartlang.org/articles/reflection-with-mirrors/">Reflectio n
2672 in Dart with Mirrors</ulink> has more information and examples. Also see
2673 the API docs for <ulink
2674 url="http://api.dartlang.org/dart_mirrors.html">dart:mirror,</ulink>
2675 especially <ulink
2676 url="http://api.dartlang.org/dart_mirrors/MirrorsUsed.html">MirrorsUsed</u link>
sethladd 2013/12/04 19:06:01 let's put a warning in and explain why and when yo
2677 (an annotation that can improve code generation), <ulink
2678 url="http://api.dartlang.org/dart_mirrors/ClassMirror.html">ClassMirror,</ ulink>
2679 and <ulink
2680 url="http://api.dartlang.org/dart_mirrors/InstanceMirror.html">InstanceMir ror.</ulink></para>
2640 </sect2> 2681 </sect2>
2641 </sect1> 2682 </sect1>
2642 2683
2643 <sect1 id="ch03-summary"> 2684 <sect1 id="ch03-summary">
2644 <title>Summary</title> 2685 <title>Summary</title>
2645 2686
2646 <para>This chapter introduced you to the most commonly used functionality 2687 <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 2688 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 2689 libraries, however. Others that you might want to look into include <ulink
2649 url="http://api.dartlang.org/dart_collection.html">dart:collection,</ulink> 2690 url="http://api.dartlang.org/dart_collection.html">dart:collection,</ulink>
2691 <ulink
2692 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 2693 <ulink url="http://api.dartlang.org/dart_js.html">dart:js,</ulink> and
2651 <ulink 2694 <ulink
2652 url="http://api.dartlang.org/dart_typed_data.html">dart:typed_data.</ulink> 2695 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 2696 You can get yet more libraries by using the pub tool, discussed in the
2654 next chapter. The <ulink 2697 next chapter. The <ulink
2655 url="http://pub.dartlang.org/packages/args">args,</ulink> <ulink 2698 url="http://pub.dartlang.org/packages/args">args,</ulink> <ulink
2656 url="http://api.dartlang.org/logging.html">logging,</ulink> <ulink 2699 url="http://api.dartlang.org/logging.html">logging,</ulink> <ulink
2657 url="http://pub.dartlang.org/packages/polymer">polymer,</ulink> and <ulink 2700 url="http://pub.dartlang.org/packages/polymer">polymer,</ulink> and <ulink
2658 url="http://api.dartlang.org/unittest.html" wordsize=""><phrase 2701 url="http://api.dartlang.org/unittest.html" wordsize=""><phrase
2659 role="keep-together">unittest</phrase></ulink> libraries are just a 2702 role="keep-together">unittest</phrase></ulink> libraries are just a
2660 sampling of what you can install using pub.</para> 2703 sampling of what you can install using pub.</para>
2661 </sect1> 2704 </sect1>
2662 </chapter> 2705 </chapter>
OLDNEW
« ch00.xml ('K') | « ch02.xml ('k') | code/ch03/mirrors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698