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

Side by Side Diff: src/site/articles/idiomatic-dart/index.html

Issue 10788006: new site (Closed) Base URL: https://code.google.com/p/dartlang-site/@master
Patch Set: final patch Created 8 years, 5 months 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
« no previous file with comments | « src/site/articles/emulating-functions/index.html ('k') | src/site/articles/index.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 --- 1 ---
2 layout: default 2 layout: default
3 title: "Idiomatic Dart" 3 title: "Idiomatic Dart"
4 rel: 4 rel:
5 author: bob-nystrom 5 author: bob-nystrom
6 --- 6 ---
7 7
8 <section> 8 <section>
9 <h1>Idiomatic Dart</h1> 9 <h1>Idiomatic Dart</h1>
10 <p> 10 <p>
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 <h2 id="top-level-definitions">Top-level definitions</h2> 251 <h2 id="top-level-definitions">Top-level definitions</h2>
252 252
253 <p>Dart is a "pure" object-oriented language in that everything you can place in a variable is a real object (no mutant "primitives") and every object is an ins tance of some class. It's not a <em>dogmatic</em> OOP language though. You aren' t required to place everything you define inside some class. Instead, you are fr ee to define functions, variables, and even getters and setters at the top level if you want.</p> 253 <p>Dart is a "pure" object-oriented language in that everything you can place in a variable is a real object (no mutant "primitives") and every object is an ins tance of some class. It's not a <em>dogmatic</em> OOP language though. You aren' t required to place everything you define inside some class. Instead, you are fr ee to define functions, variables, and even getters and setters at the top level if you want.</p>
254 254
255 {% highlight dart %} 255 {% highlight dart %}
256 num abs(num value) => value < 0 ? -value : value; 256 num abs(num value) => value < 0 ? -value : value;
257 257
258 final TWO_PI = Math.PI * 2.0; 258 final TWO_PI = Math.PI * 2.0;
259 259
260 int get today() { 260 int get today() {
261 final date = new DateTime.now(); 261 final date = new Date.now();
262 return date.day; 262 return date.day;
263 } 263 }
264 {% endhighlight dart 0 %} 264 {% endhighlight dart 0 %}
265 265
266 <p>Even in languages that don't require you to place everything inside a class o r object, like JavaScript, it's still common to do so as a form of namespacing: top-level definitions with the same name could inadvertently collide. To address that, Dart has a library system that allows you to import definitions from othe r libraries with a prefix applied to disambiguate it. That means you shouldn't < em>need</em> to defensively squirrel your definitions inside classes.</p> 266 <p>Even in languages that don't require you to place everything inside a class o r object, like JavaScript, it's still common to do so as a form of namespacing: top-level definitions with the same name could inadvertently collide. To address that, Dart has a library system that allows you to import definitions from othe r libraries with a prefix applied to disambiguate it. That means you shouldn't < em>need</em> to defensively squirrel your definitions inside classes.</p>
267 267
268 <p>We're still exploring what this actually means for how we define libraries. M ost of our code does place definitions inside classes, like Math. It's hard to t ell if this is just an ingrained habit we have from other languages or a practic e that's also good for Dart. This is an area we want feedback on.</p> 268 <p>We're still exploring what this actually means for how we define libraries. M ost of our code does place definitions inside classes, like Math. It's hard to t ell if this is just an ingrained habit we have from other languages or a practic e that's also good for Dart. This is an area we want feedback on.</p>
269 269
270 <p>We do have some examples where we use top-level definitions. The first you'll run into is <code>main()</code> which is expected to be defined at the top leve l. If you work with the DOM, the familiar <code>document</code> and <code>window </code> "variables" are actually top-level getters in Dart.</p> 270 <p>We do have some examples where we use top-level definitions. The first you'll run into is <code>main()</code> which is expected to be defined at the top leve l. If you work with the DOM, the familiar <code>document</code> and <code>window </code> "variables" are actually top-level getters in Dart.</p>
271 271
272 <h2 id="strings-and-interpolation">Strings and interpolation</h2> 272 <h2 id="strings-and-interpolation">Strings and interpolation</h2>
273 273
274 <p>Dart has a few kinds of string literals. You can use single or double quotes, and you can use triple-quoted multiline strings:</p> 274 <p>Dart has a few kinds of string literals. You can use single or double quotes, and you can use triple-quoted multiline strings:</p>
275 275
276 {% highlight dart %} 276 {% highlight dart %}
277 'I am a "string"' 277 'I am a "string"'
278 "I'm one too" 278 "I'm one too"
279 279
280 '''I'm 280 '''I'm
281 on multiple lines 281 on multiple lines
282 ''' 282 '''
283 283
284 """ 284 """
285 As 285 As
286 am 286 am
287 I 287 I
288 """ 288 """
289 {% endhighlight dart 0 %} 289 {% endhighlight dart 0 %}
290 290
291 <p>To build bigger strings from these pieces, you <em>can</em> just concatenate them using <code>+</code>: 291 <p>There is no plus (+) operator on String. It's cleaner and faster to use <em>s tring interpolation</em>:</p>
292 292
293 {% highlight dart %} 293 {% highlight dart %}
294 var name = 'Fred'; 294 var name = 'Fred';
295 var salutation = 'Hi';
296 var greeting = salutation + ', ' + name;
297 {% endhighlight dart 0 %}
298
299 <p>But it's cleaner and faster to use <em>string interpolation</em>:</p>
300
301 {% highlight dart %}
302 var name = 'Fred';
303 var salutation = 'Hi'; 295 var salutation = 'Hi';
304 var greeting = '$salutation, $name'; 296 var greeting = '$salutation, $name';
305 {% endhighlight dart 0 %} 297 {% endhighlight dart 0 %}
306 298
307 <p>A dollar sign (<code>$</code>) in a string literal followed by a variable wil l expand to that variable's value. (If the variable isn't a string, it calls <co de>toString()</code> on it.) You can also interpolate expressions by placing the m inside curly braces:</p> 299 <p>A dollar sign (<code>$</code>) in a string literal followed by a variable
300 will expand to that variable's value. (If the variable isn't a string,
301 it calls <code>toString()</code> on it.) You can also interpolate expressions by
302 placing them inside curly braces:</p>
308 303
309 {% highlight dart %} 304 {% highlight dart %}
310 var r = 2; 305 var r = 2;
311 print('The area of a circle with radius $r is ${Math.PI * r * r}'); 306 print('The area of a circle with radius $r is ${Math.PI * r * r}');
312 {% endhighlight dart 0 %} 307 {% endhighlight dart 0 %}
313 308
314 <h2 id="operators">Operators</h2> 309 <h2 id="operators">Operators</h2>
315 310
316 <p>Dart shares the same operators and precedences that you're familiar with from C, Java, etc. They will do what you expect. Under the hood, though, they are a little special. In Dart, an expression using an operator like <code>1 + 2</code> is really just syntactic sugar for calling a method. The previous example looks more like <code>1.+(2)</code> to the language.</p> 311 <p>Dart shares the same operators and precedences that you're familiar with from C, Java, etc. They will do what you expect. Under the hood, though, they are a little special. In Dart, an expression using an operator like <code>1 + 2</code> is really just syntactic sugar for calling a method. The previous example looks more like <code>1.+(2)</code> to the language.</p>
317 312
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 <li>Any number, including floating points.</li> 355 <li>Any number, including floating points.</li>
361 </ol> 356 </ol>
362 357
363 <p>Using <code>int</code> handles the first set, and using <code>num</code> hand les the second set. It's very rare that we want a number that <i>must</i> have 358 <p>Using <code>int</code> handles the first set, and using <code>num</code> hand les the second set. It's very rare that we want a number that <i>must</i> have
364 a floating point and <i>cannot</i> be an integer, which is what double expresses .</p> 359 a floating point and <i>cannot</i> be an integer, which is what double expresses .</p>
365 360
366 <p>Idiomatic Dart numbers are annotated with either <code>int</code> or <code>nu m</code>, rarely <code>double</code>.</p> 361 <p>Idiomatic Dart numbers are annotated with either <code>int</code> or <code>nu m</code>, rarely <code>double</code>.</p>
367 362
368 </section> 363 </section>
369 364
OLDNEW
« no previous file with comments | « src/site/articles/emulating-functions/index.html ('k') | src/site/articles/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698