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 2. A Tour of the Dart Language from Dart: Up and Running" | 5 title: "Chapter 2. A Tour of the Dart Language from Dart: Up and Running" |
6 description: "Read Chapter 2. A Tour of the Dart Language of Dart: Up and Runnin
g, published by O'Reilly." | 6 description: "Read Chapter 2. A Tour of the Dart Language of Dart: Up and Runnin
g, published by O'Reilly." |
7 --- | 7 --- |
8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
olspan="3" align="center">Chapter 2. A Tour of the Dart Language</th></tr><tr><t
d width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><th w
idth="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
href="ch03.html">Next</a></td></tr></table><hr></div><div class="chapter"><div c
lass="titlepage"><div><div><h1 class="title"><a name="ch02"></a>Chapter 2. A Tou
r of the Dart Language</h1></div></div></div><div class="toc"><p><b>Table of Con
tents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch02.html#main-pri
nt">A Basic Dart Program</a></span></dt><dt><span class="sect1"><a href="ch02.ht
ml#ch02-concepts">Important Concepts</a></span></dt><dd><dl><dt><span class="sec
t2"><a href="ch02.html#ch02-keywords">Keywords</a></span></dt><dt><span class="s
ect2"><a href="ch02.html#ch02-runtime-modes">Runtime Modes</a></span></dt></dl><
/dd><dt><span class="sect1"><a href="ch02.html#variables">Variables</a></span></
dt><dd><dl><dt><span class="sect2"><a href="ch02.html#variables-default-value">D
efault Value</a></span></dt><dt><span class="sect2"><a href="ch02.html#variables
-optional-types">Optional Types</a></span></dt><dt><span class="sect2"><a href="
ch02.html#ch02-final-const">Final and Const</a></span></dt></dl></dd><dt><span c
lass="sect1"><a href="ch02.html#built-in-types">Built-in Types</a></span></dt><d
d><dl><dt><span class="sect2"><a href="ch02.html#numbers">Numbers</a></span></dt
><dt><span class="sect2"><a href="ch02.html#strings">Strings</a></span></dt><dt>
<span class="sect2"><a href="ch02.html#booleans">Booleans</a></span></dt><dt><sp
an class="sect2"><a href="ch02.html#lists">Lists</a></span></dt><dt><span class=
"sect2"><a href="ch02.html#maps">Maps</a></span></dt><dt><span class="sect2"><a
href="ch02.html#ch02-symbols">Symbols</a></span></dt></dl></dd><dt><span class="
sect1"><a href="ch02.html#functions">Functions</a></span></dt><dd><dl><dt><span
class="sect2"><a href="ch02.html#functions-optional-parameters">Optional Paramet
ers</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-main">The mai
n() Function</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-func
tions-first-class">Functions as First-Class Objects</a></span></dt><dt><span cla
ss="sect2"><a href="ch02.html#ch02-lexical-scope">Lexical Scope</a></span></dt><
dt><span class="sect2"><a href="ch02.html#ch02-functions-lexical-closures">Lexic
al Closures</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-funct
ion-equality">Testing Functions for Equality</a></span></dt><dt><span class="sec
t2"><a href="ch02.html#ch02-functions-return-values">Return Values</a></span></d
t></dl></dd><dt><span class="sect1"><a href="ch02.html#operators">Operators</a><
/span></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#op-arithmetic">Ari
thmetic Operators</a></span></dt><dt><span class="sect2"><a href="ch02.html#op-e
quality">Equality and Relational Operators</a></span></dt><dt><span class="sect2
"><a href="ch02.html#ch02-op-type-test">Type Test Operators</a></span></dt><dt><
span class="sect2"><a href="ch02.html#op-assign">Assignment Operators</a></span>
</dt><dt><span class="sect2"><a href="ch02.html#op-logical">Logical Operators</a
></span></dt><dt><span class="sect2"><a href="ch02.html#op-bit">Bitwise and Shif
t Operators</a></span></dt><dt><span class="sect2"><a href="ch02.html#op-other">
Other Operators</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.h
tml#control-flow-statements">Control Flow Statements</a></span></dt><dd><dl><dt>
<span class="sect2"><a href="ch02.html#if-else">If and Else</a></span></dt><dt><
span class="sect2"><a href="ch02.html#for-loops">For Loops</a></span></dt><dt><s
pan class="sect2"><a href="ch02.html#while">While and Do-While</a></span></dt><d
t><span class="sect2"><a href="ch02.html#break">Break and Continue</a></span></d
t><dt><span class="sect2"><a href="ch02.html#switch">Switch and Case</a></span><
/dt><dt><span class="sect2"><a href="ch02.html#assert">Assert</a></span></dt></d
l></dd><dt><span class="sect1"><a href="ch02.html#exceptions">Exceptions</a></sp
an></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#ch02-exceptions-throw
">Throw</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-exception
s-catch">Catch</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-ex
ceptions-finally">Finally</a></span></dt></dl></dd><dt><span class="sect1"><a hr
ef="ch02.html#classes">Classes</a></span></dt><dd><dl><dt><span class="sect2"><a
href="ch02.html#ch02-instance-variables">Instance Variables</a></span></dt><dt>
<span class="sect2"><a href="ch02.html#ch02-constructors">Constructors</a></span
></dt><dt><span class="sect2"><a href="ch02.html#ch02-methods">Methods</a></span
></dt><dt><span class="sect2"><a href="ch02.html#ch02-class-abstract">Abstract C
lasses</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-implicit-i
nterfaces">Implicit Interfaces</a></span></dt><dt><span class="sect2"><a href="c
h02.html#ch02-class-extend">Extending a Class</a></span></dt><dt><span class="se
ct2"><a href="ch02.html#ch02-mixins">Adding Features to a Class: Mixins</a></spa
n></dt><dt><span class="sect2"><a href="ch02.html#ch02-class-vars-and-methods">C
lass Variables and Methods</a></span></dt></dl></dd><dt><span class="sect1"><a h
ref="ch02.html#generics">Generics</a></span></dt><dd><dl><dt><span class="sect2"
><a href="ch02.html#ch02-generics-why">Why Use Generics?</a></span></dt><dt><spa
n class="sect2"><a href="ch02.html#generics-literals">Using Collection Literals<
/a></span></dt><dt><span class="sect2"><a href="ch02.html#generics-constructors"
>Using Parameterized Types with Constructors</a></span></dt><dt><span class="sec
t2"><a href="ch02.html#generics-collections">Generic Collections and the Types t
hey Contain</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#
libraries">Libraries and Visibility</a></span></dt><dd><dl><dt><span class="sect
2"><a href="ch02.html#libraries-import">Using Libraries</a></span></dt><dt><span
class="sect2"><a href="ch02.html#libraries-implementing">Implementing Libraries
</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#isolates">I
solates</a></span></dt><dt><span class="sect1"><a href="ch02.html#typedefs">Type
defs</a></span></dt><dt><span class="sect1"><a href="ch02.html#ch02-metadata">Me
tadata</a></span></dt><dt><span class="sect1"><a href="ch02.html#comments">Comme
nts</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#ch02-comme
nts-single-line">Single-Line Comments</a></span></dt><dt><span class="sect2"><a
href="ch02.html#ch02-comments-multi-line">Multi-Line Comments</a></span></dt><dt
><span class="sect2"><a href="ch02.html#comments-doc">Documentation Comments</a>
</span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#ch02-summary">S
ummary</a></span></dt></dl></div><p>This chapter shows you how to use each major
Dart feature, from | 8 <div class="navheader"><table width="100%" summary="Navigation header"><tr><th c
olspan="3" align="center">Chapter 2. A Tour of the Dart Language</th></tr><tr><t
d width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><th w
idth="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n"
href="ch03.html">Next</a></td></tr></table><hr></div><div class="chapter"><div c
lass="titlepage"><div><div><h1 class="title"><a name="ch02"></a>Chapter 2. A Tou
r of the Dart Language</h1></div></div></div><div class="toc"><p><b>Table of Con
tents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch02.html#main-pri
nt">A Basic Dart Program</a></span></dt><dt><span class="sect1"><a href="ch02.ht
ml#ch02-concepts">Important Concepts</a></span></dt><dd><dl><dt><span class="sec
t2"><a href="ch02.html#ch02-keywords">Keywords</a></span></dt><dt><span class="s
ect2"><a href="ch02.html#ch02-runtime-modes">Runtime Modes</a></span></dt></dl><
/dd><dt><span class="sect1"><a href="ch02.html#variables">Variables</a></span></
dt><dd><dl><dt><span class="sect2"><a href="ch02.html#variables-default-value">D
efault Value</a></span></dt><dt><span class="sect2"><a href="ch02.html#variables
-optional-types">Optional Types</a></span></dt><dt><span class="sect2"><a href="
ch02.html#ch02-final-const">Final and Const</a></span></dt></dl></dd><dt><span c
lass="sect1"><a href="ch02.html#built-in-types">Built-in Types</a></span></dt><d
d><dl><dt><span class="sect2"><a href="ch02.html#numbers">Numbers</a></span></dt
><dt><span class="sect2"><a href="ch02.html#strings">Strings</a></span></dt><dt>
<span class="sect2"><a href="ch02.html#booleans">Booleans</a></span></dt><dt><sp
an class="sect2"><a href="ch02.html#lists">Lists</a></span></dt><dt><span class=
"sect2"><a href="ch02.html#maps">Maps</a></span></dt><dt><span class="sect2"><a
href="ch02.html#ch02-symbols">Symbols</a></span></dt></dl></dd><dt><span class="
sect1"><a href="ch02.html#functions">Functions</a></span></dt><dd><dl><dt><span
class="sect2"><a href="ch02.html#functions-optional-parameters">Optional Paramet
ers</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-main">The mai
n() Function</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-func
tions-first-class">Functions as First-Class Objects</a></span></dt><dt><span cla
ss="sect2"><a href="ch02.html#ch02-lexical-scope">Lexical Scope</a></span></dt><
dt><span class="sect2"><a href="ch02.html#ch02-functions-lexical-closures">Lexic
al Closures</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-funct
ion-equality">Testing Functions for Equality</a></span></dt><dt><span class="sec
t2"><a href="ch02.html#ch02-functions-return-values">Return Values</a></span></d
t></dl></dd><dt><span class="sect1"><a href="ch02.html#operators">Operators</a><
/span></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#op-arithmetic">Ari
thmetic Operators</a></span></dt><dt><span class="sect2"><a href="ch02.html#op-e
quality">Equality and Relational Operators</a></span></dt><dt><span class="sect2
"><a href="ch02.html#ch02-op-type-test">Type Test Operators</a></span></dt><dt><
span class="sect2"><a href="ch02.html#op-assign">Assignment Operators</a></span>
</dt><dt><span class="sect2"><a href="ch02.html#op-logical">Logical Operators</a
></span></dt><dt><span class="sect2"><a href="ch02.html#op-bit">Bitwise and Shif
t Operators</a></span></dt><dt><span class="sect2"><a href="ch02.html#op-other">
Other Operators</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.h
tml#control-flow-statements">Control Flow Statements</a></span></dt><dd><dl><dt>
<span class="sect2"><a href="ch02.html#if-else">If and Else</a></span></dt><dt><
span class="sect2"><a href="ch02.html#for-loops">For Loops</a></span></dt><dt><s
pan class="sect2"><a href="ch02.html#while">While and Do-While</a></span></dt><d
t><span class="sect2"><a href="ch02.html#break">Break and Continue</a></span></d
t><dt><span class="sect2"><a href="ch02.html#switch">Switch and Case</a></span><
/dt><dt><span class="sect2"><a href="ch02.html#assert">Assert</a></span></dt></d
l></dd><dt><span class="sect1"><a href="ch02.html#exceptions">Exceptions</a></sp
an></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#ch02-exceptions-throw
">Throw</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-exception
s-catch">Catch</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-ex
ceptions-finally">Finally</a></span></dt></dl></dd><dt><span class="sect1"><a hr
ef="ch02.html#classes">Classes</a></span></dt><dd><dl><dt><span class="sect2"><a
href="ch02.html#ch02-instance-variables">Instance Variables</a></span></dt><dt>
<span class="sect2"><a href="ch02.html#ch02-constructors">Constructors</a></span
></dt><dt><span class="sect2"><a href="ch02.html#ch02-methods">Methods</a></span
></dt><dt><span class="sect2"><a href="ch02.html#ch02-class-abstract">Abstract C
lasses</a></span></dt><dt><span class="sect2"><a href="ch02.html#ch02-implicit-i
nterfaces">Implicit Interfaces</a></span></dt><dt><span class="sect2"><a href="c
h02.html#ch02-class-extend">Extending a Class</a></span></dt><dt><span class="se
ct2"><a href="ch02.html#ch02-mixins">Adding Features to a Class: Mixins</a></spa
n></dt><dt><span class="sect2"><a href="ch02.html#ch02-class-vars-and-methods">C
lass Variables and Methods</a></span></dt></dl></dd><dt><span class="sect1"><a h
ref="ch02.html#generics">Generics</a></span></dt><dd><dl><dt><span class="sect2"
><a href="ch02.html#ch02-generics-why">Why Use Generics?</a></span></dt><dt><spa
n class="sect2"><a href="ch02.html#generics-literals">Using Collection Literals<
/a></span></dt><dt><span class="sect2"><a href="ch02.html#generics-constructors"
>Using Parameterized Types with Constructors</a></span></dt><dt><span class="sec
t2"><a href="ch02.html#generics-collections">Generic Collections and the Types t
hey Contain</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#
libraries">Libraries and Visibility</a></span></dt><dd><dl><dt><span class="sect
2"><a href="ch02.html#libraries-import">Using Libraries</a></span></dt><dt><span
class="sect2"><a href="ch02.html#libraries-implementing">Implementing Libraries
</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#isolates">I
solates</a></span></dt><dt><span class="sect1"><a href="ch02.html#typedefs">Type
defs</a></span></dt><dt><span class="sect1"><a href="ch02.html#ch02-metadata">Me
tadata</a></span></dt><dt><span class="sect1"><a href="ch02.html#comments">Comme
nts</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch02.html#ch02-comme
nts-single-line">Single-Line Comments</a></span></dt><dt><span class="sect2"><a
href="ch02.html#ch02-comments-multi-line">Multi-Line Comments</a></span></dt><dt
><span class="sect2"><a href="ch02.html#comments-doc">Documentation Comments</a>
</span></dt></dl></dd><dt><span class="sect1"><a href="ch02.html#ch02-summary">S
ummary</a></span></dt></dl></div><p>This chapter shows you how to use each major
Dart feature, from |
9 variables and operators to classes and libraries, with the assumption that | 9 variables and operators to classes and libraries, with the assumption that |
10 you already know how to program in another language.</p><div class="note" styl
e="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>To pl
ay with each feature, create a command-line application project | 10 you already know how to program in another language.</p><div class="note" styl
e="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>To pl
ay with each feature, create a command-line application project |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 mode. In checked mode, <code class="literal">assert(<em class="replaceab
le"><code>condition</code></em>)</code> | 88 mode. In checked mode, <code class="literal">assert(<em class="replaceab
le"><code>condition</code></em>)</code> |
89 throws an exception unless <em class="replaceable"><code>condition</code
></em> is | 89 throws an exception unless <em class="replaceable"><code>condition</code
></em> is |
90 true. For details, see <a class="xref" href="ch02.html#assert" title="As
sert">the section called “Assert”</a>.</p></div></div><div class="sect2"><div cl
ass="titlepage"><div><div><h3 class="title"><a name="variables-optional-types"><
/a>Optional Types</h3></div></div></div><p>You have the option of adding static
types to your variable | 90 true. For details, see <a class="xref" href="ch02.html#assert" title="As
sert">the section called “Assert”</a>.</p></div></div><div class="sect2"><div cl
ass="titlepage"><div><div><h3 class="title"><a name="variables-optional-types"><
/a>Optional Types</h3></div></div></div><p>You have the option of adding static
types to your variable |
91 declarations:</p><pre class="programlisting"><em><span class="remark">lang
-dart | 91 declarations:</p><pre class="programlisting"><em><span class="remark">lang
-dart |
92 </span></em>String name = 'Bob';</pre><p>Adding types is a way to clearly expres
s your intent. Tools such | 92 </span></em>String name = 'Bob';</pre><p>Adding types is a way to clearly expres
s your intent. Tools such |
93 as compilers and editors can use these types to help you, by providing | 93 as compilers and editors can use these types to help you, by providing |
94 code completion and early warnings for bugs and code <span class="keep-tog
ether">completion</span>.</p><div class="note" style="margin-left: 0.5in; margin
-right: 0.5in;"><h3 class="title">Note</h3><p>This chapter follows the <a class=
"ulink" href="http://www.dartlang.org/articles/style-guide/#type-annotations" ta
rget="_top">style | 94 code completion and early warnings for bugs and code <span class="keep-tog
ether">completion</span>.</p><div class="note" style="margin-left: 0.5in; margin
-right: 0.5in;"><h3 class="title">Note</h3><p>This chapter follows the <a class=
"ulink" href="http://www.dartlang.org/articles/style-guide/#type-annotations" ta
rget="_top">style |
95 guide recommendation</a> of using <code class="literal">var</code>, rath
er than type annotations, for local | 95 guide recommendation</a> of using <code class="literal">var</code>, rath
er than type annotations, for local |
96 variables.</p></div></div><div class="sect2"><div class="titlepage"><div
><div><h3 class="title"><a name="ch02-final-const"></a>Final and Const</h3></div
></div></div><p>If you never intend to change a variable, use <code class="liter
al">final</code> or <code class="literal">const</code>, either instead of <code
class="literal">var</code> or in addition to a type. A final variable | 96 variables.</p></div></div><div class="sect2"><div class="titlepage"><div
><div><h3 class="title"><a name="ch02-final-const"></a>Final and Const</h3></div
></div></div><p>If you never intend to change a variable, use <code class="liter
al">final</code> or <code class="literal">const</code>, either instead of <code
class="literal">var</code> or in addition to a type. A final variable |
97 can be set only once; a const variable is a compile-time | 97 can be set only once; a const variable is a compile-time |
98 constant.</p><p>A local, top-level, or class variable that’s declared as <
code class="literal">final</code> is initialized the first time it’s | 98 constant.</p><p>A top-level or class variable that’s declared as <code cla
ss="literal">final</code> is initialized the first time it’s |
99 used:</p><pre class="programlisting"><em><span class="remark">lang-dart | 99 used:</p><pre class="programlisting"><em><span class="remark">lang-dart |
100 </span></em>final name = 'Bob'; // Or: final String name = 'Bob'; | 100 </span></em>final name = 'Bob'; // Or: final String name = 'Bob'; |
101 // name = 'Alice'; // Uncommenting this results in an error</pre><div class="
note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h
3><p>Lazy initialization of final variables helps apps start up | 101 // name = 'Alice'; // Uncommenting this results in an error</pre><div class="
note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h
3><p>Lazy initialization of final variables helps apps start up |
102 faster.</p></div><p>Use <code class="literal">const</code> for variables
that | 102 faster.</p></div><p>Use <code class="literal">const</code> for variables
that |
103 you want to be compile-time constants. If the const variable is at the | 103 you want to be compile-time constants. If the const variable is at the |
104 class level, mark it <code class="literal">static const</code>. (Instance | 104 class level, mark it <code class="literal">static const</code>. (Instance |
105 variables can’t be const.) Where you declare the variable, set the value | 105 variables can’t be const.) Where you declare the variable, set the value |
106 to a compile-time constant such as a literal, a const variable, or the | 106 to a compile-time constant such as a literal, a const variable, or the |
107 result of an arithmetic operation on constant numbers:</p><pre class="prog
ramlisting"><em><span class="remark">lang-dart | 107 result of an arithmetic operation on constant numbers:</p><pre class="prog
ramlisting"><em><span class="remark">lang-dart |
108 </span></em>const bar = 1000000; // Unit of pressure (in dynes/cm2) | 108 </span></em>const bar = 1000000; // Unit of pressure (in dynes/cm2) |
109 const atm = 1.01325 * bar; // Standard atmosphere</pre></div></div><div class="s
ect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a
name="built-in-types"></a>Built-in Types</h2></div></div></div><p>The Dart langu
age has special support for the following | 109 const atm = 1.01325 * bar; // Standard atmosphere</pre></div></div><div class="s
ect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a
name="built-in-types"></a>Built-in Types</h2></div></div></div><p>The Dart langu
age has special support for the following |
110 types:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-sty
le-type: disc; "><li class="listitem"><p>numbers</p></li><li class="listitem"><p
>strings</p></li><li class="listitem"><p>booleans</p></li><li class="listitem"><
p>lists (also known as <span class="emphasis"><em>arrays</em></span>)</p></li><l
i class="listitem"><p>maps</p></li><li class="listitem"><p>symbols</p></li></ul>
</div><p>You can initialize an object of any of these special types using a | 110 types:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-sty
le-type: disc; "><li class="listitem"><p>numbers</p></li><li class="listitem"><p
>strings</p></li><li class="listitem"><p>booleans</p></li><li class="listitem"><
p>lists (also known as <span class="emphasis"><em>arrays</em></span>)</p></li><l
i class="listitem"><p>maps</p></li><li class="listitem"><p>symbols</p></li></ul>
</div><p>You can initialize an object of any of these special types using a |
111 literal. For example, <code class="literal">'this is a | 111 literal. For example, <code class="literal">'this is a |
112 string'</code> is a string literal, and <code class="literal">true</code> is
a boolean literal.</p><p>Because every variable in Dart refers to an object—an
instance of a | 112 string'</code> is a string literal, and <code class="literal">true</code> is
a boolean literal.</p><p>Because every variable in Dart refers to an object—an
instance of a |
113 <span class="emphasis"><em>class</em></span>—you can usually use | 113 <span class="emphasis"><em>class</em></span>—you can usually use |
114 <span class="emphasis"><em>constructors</em></span> to initialize variables.
Some of the | 114 <span class="emphasis"><em>constructors</em></span> to initialize variables.
Some of the |
115 built-in types have their own constructors. For example, you can use the | 115 built-in types have their own constructors. For example, you can use the |
116 <code class="literal">Map()</code> constructor to create a map, using code s
uch as | 116 <code class="literal">Map()</code> constructor to create a map, using code s
uch as |
117 <code class="literal">new Map()</code>.</p><div class="sect2"><div class="ti
tlepage"><div><div><h3 class="title"><a name="numbers"></a>Numbers</h3></div></d
iv></div><p>Dart numbers come in two flavors:</p><div class="variablelist"><dl c
lass="variablelist"><dt><span class="term"><a class="ulink" href="http://api.dar
tlang.org/dart_core/int.html" target="_top"> | 117 <code class="literal">new Map()</code>.</p><div class="sect2"><div class="ti
tlepage"><div><div><h3 class="title"><a name="numbers"></a>Numbers</h3></div></d
iv></div><p>Dart numbers come in two flavors:</p><div class="variablelist"><dl c
lass="variablelist"><dt><span class="term"><a class="ulink" href="http://api.dar
tlang.org/dart_core/int.html" target="_top"> |
118 <code class="literal">int</code> </a></span></dt><dd><p>Integer values
, which generally should be in the range | 118 <code class="literal">int</code> </a></span></dt><dd><p>Integer values
, which generally should be in the range |
119 -2<sup>53</sup> to 2<sup>53</sup> | 119 -2<sup>53</sup> to |
120 </p></dd><dt><span class="term"><a class="ulink" href="http://api.da
rtlang.org/dart_core/double.html" target="_top"> | 120 2<sup>53</sup></p></dd><dt><span class="term"><a class="ulink" href=
"http://api.dartlang.org/dart_core/double.html" target="_top"> |
121 <code class="literal">double</code> </a></span></dt><dd><p>64-bit (dou
ble-precision) floating-point numbers, as | 121 <code class="literal">double</code> </a></span></dt><dd><p>64-bit (dou
ble-precision) floating-point numbers, as |
122 specified by the IEEE 754 standard</p></dd></dl></div><p>Both <code
class="literal">int</code> and <code class="literal">double</code> are subtypes
of <a class="ulink" href="http://api.dartlang.org/dart_core/num.html" target="_t
op"><code class="literal">num</code>.</a> The num type includes basic | 122 specified by the IEEE 754 standard</p></dd></dl></div><p>Both <code
class="literal">int</code> and <code class="literal">double</code> are subtypes
of <a class="ulink" href="http://api.dartlang.org/dart_core/num.html" target="_t
op"><code class="literal">num</code>.</a> The num type includes basic |
123 operators such as +, -, /, and *, as well as bitwise operators such as | 123 operators such as +, -, /, and *, as well as bitwise operators such as |
124 >>. The num type is also where you’ll find | 124 >>. The num type is also where you’ll find |
125 <code class="literal">abs()</code>,<code class="literal"> ceil()</code>, a
nd | 125 <code class="literal">abs()</code>,<code class="literal"> ceil()</code>, a
nd |
126 <code class="literal">floor()</code>, among other methods. If num and its
subtypes | 126 <code class="literal">floor()</code>, among other methods. If num and its
subtypes |
127 don’t have what you’re looking for, the <a class="ulink" href="http://api.
dartlang.org/dart_math.html" target="_top">Math</a> library might. | 127 don’t have what you’re looking for, the <a class="ulink" href="http://api.
dartlang.org/dart_math.html" target="_top">Math</a> library |
128 </p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Warning</h3><p>Integers outside of the -2<sup>53</sup> to | 128 might.</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.
5in;"><h3 class="title">Warning</h3><p>Integers outside of the -2<sup>53</sup> t
o |
129 2<sup>53</sup> range currently behave differently in | 129 2<sup>53</sup> range currently behave differently in |
130 JavaScript produced from Dart code than they do when the same Dart | 130 JavaScript produced from Dart code than they do when the same Dart |
131 code runs in the Dart VM. The reason is that Dart is specified to have | 131 code runs in the Dart VM. The reason is that Dart is specified to have |
132 arbitrary-precision integers, but JavaScript isn't. See <a class="ulink"
href="http://dartbug.com/1533" target="_top">issue 1533</a> for details.</p></d
iv><p>Integers are numbers without a decimal point. Here are some | 132 arbitrary-precision integers, but JavaScript isn't. See <a class="ulink"
href="http://dartbug.com/1533" target="_top">issue 1533</a> for details.</p></d
iv><p>Integers are numbers without a decimal point. Here are some |
133 examples of defining integer literals:</p><pre class="programlisting"><em>
<span class="remark">lang-dart | 133 examples of defining integer literals:</p><pre class="programlisting"><em>
<span class="remark">lang-dart |
134 </span></em>var x = 1; | 134 </span></em>var x = 1; |
135 var hex = 0xDEADBEEF; | 135 var hex = 0xDEADBEEF; |
136 var bigInt = 346534658346524376592384765923749587398457294759347029438709349347;
</pre><p>If a number includes a decimal, it is a double. Here are some | 136 var bigInt = 346534658346524376592384765923749587398457294759347029438709349347;
</pre><p>If a number includes a decimal, it is a double. Here are some |
137 examples of defining double literals:</p><pre class="programlisting"><em><
span class="remark">lang-dart | 137 examples of defining double literals:</p><pre class="programlisting"><em><
span class="remark">lang-dart |
138 </span></em>var y = 1.1; | 138 </span></em>var y = 1.1; |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 indicating that something unexpected happened. If the exception isn’t | 745 indicating that something unexpected happened. If the exception isn’t |
746 caught, the isolate that raised the exception is suspended, and typically | 746 caught, the isolate that raised the exception is suspended, and typically |
747 the isolate and its program are terminated.</p><p>In contrast to Java, all o
f Dart’s exceptions are unchecked | 747 the isolate and its program are terminated.</p><p>In contrast to Java, all o
f Dart’s exceptions are unchecked |
748 exceptions. Methods do not declare which exceptions they might throw, and | 748 exceptions. Methods do not declare which exceptions they might throw, and |
749 you are not required to catch any <span class="keep-together">exceptions</sp
an>.</p><p>Dart provides <a class="ulink" href="http://api.dartlang.org/dart_cor
e/Exception.html" target="_top">Exception</a> | 749 you are not required to catch any <span class="keep-together">exceptions</sp
an>.</p><p>Dart provides <a class="ulink" href="http://api.dartlang.org/dart_cor
e/Exception.html" target="_top">Exception</a> |
750 and <a class="ulink" href="http://api.dartlang.org/docs/continuous/dart_core
/Error.html" target="_top">Error</a> | 750 and <a class="ulink" href="http://api.dartlang.org/docs/continuous/dart_core
/Error.html" target="_top">Error</a> |
751 types, as well as numerous predefined subtypes. You can, of course, define | 751 types, as well as numerous predefined subtypes. You can, of course, define |
752 your own exceptions. However, Dart programs can throw any non-null | 752 your own exceptions. However, Dart programs can throw any non-null |
753 object—not just Exception and Error objects—as an exception.</p><div class="
sect2"><div class="titlepage"><div><div><h3 class="title"><a name="ch02-exceptio
ns-throw"></a>Throw</h3></div></div></div><p>Here’s an example of throwing, or <
span class="emphasis"><em>raising</em></span>, an | 753 object—not just Exception and Error objects—as an exception.</p><div class="
sect2"><div class="titlepage"><div><div><h3 class="title"><a name="ch02-exceptio
ns-throw"></a>Throw</h3></div></div></div><p>Here’s an example of throwing, or <
span class="emphasis"><em>raising</em></span>, an |
754 exception:</p><pre class="programlisting"><em><span class="remark">lang-da
rt | 754 exception:</p><pre class="programlisting"><em><span class="remark">lang-da
rt |
755 </span></em>throw new ExpectException('Value must be greater than zero');</pre><
p>You can also throw arbitrary objects:</p><pre class="programlisting"><em><span
class="remark">lang-dart | 755 </span></em>throw new FormatException('expected at least one section');</pre><p>
You can also throw arbitrary objects:</p><pre class="programlisting"><em><span c
lass="remark">lang-dart |
756 </span></em>throw 'Out of llamas!';</pre><p>Because throwing an exception is an
expression, you can throw | 756 </span></em>throw 'Out of llamas!';</pre><p>Because throwing an exception is an
expression, you can throw |
757 exceptions in => statements, as well as anywhere else that allows | 757 exceptions in => statements, as well as anywhere else that allows |
758 expressions:</p><pre class="programlisting"><em><span class="remark">lang-
dart | 758 expressions:</p><pre class="programlisting"><em><span class="remark">lang-
dart |
759 </span></em>distanceTo(Point other) => throw new UnimplementedError();</pre><
/div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a na
me="ch02-exceptions-catch"></a>Catch</h3></div></div></div><p>Catching, or captu
ring, an exception stops the exception from | 759 </span></em>distanceTo(Point other) => throw new UnimplementedError();</pre><
/div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a na
me="ch02-exceptions-catch"></a>Catch</h3></div></div></div><p>Catching, or captu
ring, an exception stops the exception from |
760 propagating. Catching an exception gives you a chance to handle | 760 propagating. Catching an exception gives you a chance to handle |
761 it:</p><pre class="programlisting"><em><span class="remark">lang-dart | 761 it:</p><pre class="programlisting"><em><span class="remark">lang-dart |
762 </span></em>try { | 762 </span></em>try { |
763 breedMoreLlamas(); | 763 breedMoreLlamas(); |
764 } on OutOfLlamasException { | 764 } on OutOfLlamasException { |
765 buyMoreLlamas(); | 765 buyMoreLlamas(); |
(...skipping 815 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1581 // ... | 1581 // ... |
1582 } | 1582 } |
1583 | 1583 |
1584 /// Exercises your llama with an [activity] for | 1584 /// Exercises your llama with an [activity] for |
1585 /// [timeLimit] minutes. | 1585 /// [timeLimit] minutes. |
1586 void exercise(Activity activity, int timeLimit) { | 1586 void exercise(Activity activity, int timeLimit) { |
1587 // ... | 1587 // ... |
1588 } | 1588 } |
1589 }</pre><p>In the generated documentation, <code class="literal">[Food]</code> be
comes a link to the API docs for the | 1589 }</pre><p>In the generated documentation, <code class="literal">[Food]</code> be
comes a link to the API docs for the |
1590 Food class.</p><p>To parse Dart code and generate HTML documentation, you
can use | 1590 Food class.</p><p>To parse Dart code and generate HTML documentation, you
can use |
1591 the SDK’s | 1591 the SDK’s <a class="ulink" href="http://www.dartlang.org/tools/docgen/" ta
rget="_top">documentation generation |
1592 <a class="ulink" href="http://www.dartlang.org/tools/docgen/" target="_top
">documentation generation tool.</a> For an example of generated | 1592 tool.</a> For an example of generated documentation, see the <a class="uli
nk" href="http://api.dartlang.org" target="_top">Dart API documentation.</a> For
advice |
1593 documentation, see the <a class="ulink" href="http://api.dartlang.org" tar
get="_top">Dart API | 1593 on how to structure your comments, see <a class="ulink" href="http://www.d
artlang.org/articles/doc-comment-guidelines/" target="_top">Guidelines |
1594 documentation.</a> For advice on how to structure your comments, see | |
1595 <a class="ulink" href="http://www.dartlang.org/articles/doc-comment-guidel
ines/" target="_top">Guidelines | |
1596 for Dart Doc Comments.</a></p></div></div><div class="sect1"><div class="t
itlepage"><div><div><h2 class="title" style="clear: both"><a name="ch02-summary"
></a>Summary</h2></div></div></div><p>This chapter summarized the commonly used
features in the Dart | 1594 for Dart Doc Comments.</a></p></div></div><div class="sect1"><div class="t
itlepage"><div><div><h2 class="title" style="clear: both"><a name="ch02-summary"
></a>Summary</h2></div></div></div><p>This chapter summarized the commonly used
features in the Dart |
1597 language. More features are being implemented, but we expect | 1595 language. More features are being implemented, but we expect that they |
1598 that they won’t break existing code. For more information, see the <a class=
"ulink" href="http://www.dartlang.org/docs/spec/" target="_top">Dart Language | 1596 won’t break existing code. For more information, see the <a class="ulink" hr
ef="http://www.dartlang.org/docs/spec/" target="_top">Dart Language |
1599 Specification</a> and <a class="ulink" href="http://www.dartlang.org/article
s/" target="_top">articles</a> such as <a class="ulink" href="http://www.dartlan
g.org/articles/idiomatic-dart/" target="_top">Idiomatic Dart.</a></p></div><div
class="footnotes"><br><hr style="width:100; text-align:left;margin-left: 0"><div
id="ftn.ch02-footnote-1" class="footnote"><p><a href="#ch02-footnote-1" class="
para"><sup class="para">[1] </sup></a>URI stands for <em class="firstterm">unifo
rm resource | 1597 Specification</a> and <a class="ulink" href="http://www.dartlang.org/article
s/" target="_top">articles</a> such as <a class="ulink" href="http://www.dartlan
g.org/articles/idiomatic-dart/" target="_top">Idiomatic Dart.</a></p></div><div
class="footnotes"><br><hr style="width:100; text-align:left;margin-left: 0"><div
id="ftn.ch02-footnote-1" class="footnote"><p><a href="#ch02-footnote-1" class="
para"><sup class="para">[1] </sup></a>URI stands for <em class="firstterm">unifo
rm resource |
1600 identifier</em>. URLs (<em class="firstterm">uniform resource | 1598 identifier</em>. URLs (<em class="firstterm">uniform resource |
1601 locators</em>) are a common kind of URI.</p></div></div></div><div cla
ss="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width
="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><td width="2
0%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="c
h03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapte
r 1. Quick Start </td><td width="20%" align="center"><a accesskey="h" href="inde
x.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 3. A To
ur of the Dart Libraries</td></tr></table></div> | 1599 locators</em>) are a common kind of URI.</p></div></div></div><div cla
ss="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width
="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><td width="2
0%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="c
h03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapte
r 1. Quick Start </td><td width="20%" align="center"><a accesskey="h" href="inde
x.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 3. A To
ur of the Dart Libraries</td></tr></table></div> |
OLD | NEW |