Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embrace optional new/const in the language tour #940

Merged
merged 17 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,46 @@ The original sources for the (large) samples were copied from the

## New sources

Consolidated and reworked versions of the original sources have been developed,
and are found under these folders:
Consolidated and reworked versions of the original sources
are under these folders:

- `examples/*/lib`
- `examples/*/test`

As can be expected, Travis jobs run the
Travis jobs run the following:

- Analyzer over both `lib` and `test`
- Tests under `test`

You can run some or all of these at the command line, as well.

To run only the analyzer:

```
cd examples
dartanalyzer --preview-dart-2 .
```

To run both the analyzer and tests:

```
./scripts/analyze-and-test-examples.sh`
```

If you get a warning about test failures or analysis errors,
you might need to update one or more analyzer results files
(for example, `examples/misc/analyzer-2-results.txt`).

To update an analyzer results file:

1. Run `./scripts/analyze-and-test-examples.sh --save-logs`.
2. Look at the diffs for the results files.
3. If the diffs look good but some comments are missing,
add back the comments that are still relevant.
4. Run `./scripts/analyze-and-test-examples.sh` to confirm that
your changes are good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these helpful instructions.


### File organisation for the Tours

The new Language Tour sources are under `lib/language_tour` and `test/language_tour`.
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/analyzer-2-results.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Analyzing lib, test...
# https://github.com/dart-lang/sdk/issues/32236 - flow analysis can't yet figure out that the variable is of type Person.
error • 'Object' doesn't extend 'SomeBaseClass' at lib/language_tour/generics/misc.dart:28:23 • type_argument_not_matching_bounds
error • 'Object' doesn't extend 'SomeBaseClass' at lib/language_tour/generics/misc.dart:28:19 • type_argument_not_matching_bounds
error • A value of type 'dynamic' can't be assigned to a variable of type 'Person' at lib/library_tour/core/hash_code.dart:25:21 • invalid_assignment
2 errors found.
10 changes: 5 additions & 5 deletions examples/misc/lib/language_tour/built_in_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ void miscDeclAnalyzedButNotTested() {

{
// #docregion map-constructor
var gifts = new Map();
var gifts = Map();
gifts['first'] = 'partridge';
gifts['second'] = 'turtledoves';
gifts['fifth'] = 'golden rings';

var nobleGases = new Map();
var nobleGases = Map();
nobleGases[2] = 'helium';
nobleGases[10] = 'neon';
nobleGases[18] = 'argon';
Expand Down Expand Up @@ -156,9 +156,9 @@ class RunesExample {
print(clapping.codeUnits);
print(clapping.runes.toList());

Runes input = new Runes(
'\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(new String.fromCharCodes(input));
Runes input =
Runes('\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(String.fromCharCodes(input));
}
// #enddocregion runes
}
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/callable_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class WannabeFunction {
String call(String a, String b, String c) => '$a $b $c!';
}

var wf = new WannabeFunction();
var wf = WannabeFunction();
var out = wf('Hi', 'there,', 'gang');

main() => print(out);
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/classes/employee.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Employee extends Person {
// #enddocregion method-then-constructor

void main() {
var emp = new Employee.fromJson({});
var emp = Employee.fromJson({});
// Prints:
// in Person
// in Employee
Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/classes/impostor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class Impostor implements Person {
String greetBob(Person person) => person.greet('Bob');

void main() {
print(greetBob(new Person('Kathy')));
print(greetBob(new Impostor()));
print(greetBob(Person('Kathy')));
print(greetBob(Impostor()));
}
10 changes: 5 additions & 5 deletions examples/misc/lib/language_tour/classes/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Logger {
if (_cache.containsKey(name)) {
return _cache[name];
} else {
final logger = new Logger._internal(name);
final logger = Logger._internal(name);
_cache[name] = logger;
return logger;
}
Expand All @@ -28,13 +28,13 @@ class Logger {

void main() {
// #docregion logger
var logger = new Logger('UI');
var logger = Logger('UI');
logger.log('Button clicked');
// #enddocregion logger

var l1 = new Logger('log1');
var l2 = new Logger('log1');
var l3 = new Logger('log2');
var l1 = Logger('log1');
var l2 = Logger('log1');
var l3 = Logger('log2');

assert(identical(l1, l2));
assert(l1 != l3);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
void main() {
dynamic a = new A();
dynamic a = A();
a.foo();
}

Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/classes/orchestra.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class Maestro extends Person
// #enddocregion Musician-and-Maestro

void main() {
var director = new Maestro('Allen');
var director = Maestro('Allen');
director.entertainMe(); // Waving hands

var musician = new Musician('Kathy');
var musician = Musician('Kathy');
musician.canPlayPiano = true;
musician.entertainMe(); // Playing piano
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Point {
}

void main() {
var p = new Point(2, 3);
var p = Point(2, 3);
print(p.distanceFromOrigin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Point {
}

void main() {
var a = new Point(2, 2);
var b = new Point(4, 4);
var a = Point(2, 2);
var b = Point(4, 4);
var distance = Point.distanceBetween(a, b);
assert(2.8 < distance && distance < 2.9);
print(distance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Point {
// #enddocregion class

void main() {
var point = new Point();
var point = Point();
point.x = 4; // Use the setter method for x.
assert(point.x == 4); // Use the getter method for x.
assert(point.y == null); // Values default to null.
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/classes/proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ignore_for_file: deprecated_member_use

void main() {
dynamic a = new A();
dynamic a = A();
a.doSomething();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/classes/proxy_alt.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
void main() {
dynamic a = new A();
dynamic a = A();
a.doSomething();
a.doSomeOtherThing();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/classes/rectangle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Rectangle {
}

void main() {
var rect = new Rectangle(3, 4, 20, 15);
var rect = Rectangle(3, 4, 20, 15);
assert(rect.left == 3);
rect.right = 12;
assert(rect.left == -8);
Expand Down
8 changes: 4 additions & 4 deletions examples/misc/lib/language_tour/classes/vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ class Vector {

/// Overrides + (a + b).
Vector operator +(Vector v) {
return new Vector(x + v.x, y + v.y);
return Vector(x + v.x, y + v.y);
}

/// Overrides - (a - b).
Vector operator -(Vector v) {
return new Vector(x - v.x, y - v.y);
return Vector(x - v.x, y - v.y);
}
}

void main() {
final v = new Vector(2, 3);
final w = new Vector(2, 2);
final v = Vector(2, 3);
final w = Vector(2, 2);

// v == (2, 3)
assert(v.x == 2 && v.y == 3);
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/comments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void miscDeclAnalyzedButNotTested() {
/*
* This is a lot of work. Consider raising chickens.

Llama larry = new Llama();
Llama larry = Llama();
larry.feed();
larry.exercise();
larry.clean();
Expand Down
5 changes: 2 additions & 3 deletions examples/misc/lib/language_tour/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void cleanLlamaStalls() {}
void miscDeclAnalyzedButNotTested(bool c) {
if (c) {
// #docregion throw-FormatException
throw new FormatException('Expected at least 1 section');
throw FormatException('Expected at least 1 section');
// #enddocregion throw-FormatException
}

Expand All @@ -23,8 +23,7 @@ void miscDeclAnalyzedButNotTested(bool c) {

{
// #docregion throw-is-an-expression
void distanceTo(Point other) =>
throw new UnimplementedError();
void distanceTo(Point other) => throw UnimplementedError();
// #enddocregion throw-is-an-expression
}

Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/function_equality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void main() {
assert(A.bar == x);

// Comparing instance methods.
var v = new A(); // Instance #1 of A
var w = new A(); // Instance #2 of A
var v = A(); // Instance #1 of A
var w = A(); // Instance #2 of A
var y = w;
x = w.baz;

Expand Down
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/generics/misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'base_class.dart';
void miscDeclAnalyzedButNotTested() {
{
// #docregion why-generics
var names = new List<String>();
var names = List<String>();
names.addAll(['Seth', 'Kathy', 'Lars']);
names.add(42); // Error // ignore: argument_type_not_assignable
// #enddocregion why-generics
Expand All @@ -25,7 +25,7 @@ void miscDeclAnalyzedButNotTested() {
// ignore_for_file: 2, type_argument_not_matching_bounds
// Specifying any non-SomeBaseClass type results in an error.
// #docregion Foo-Object-error
var foo = new Foo<Object>(); //!analysis-issue
var foo = Foo<Object>(); //!analysis-issue
// #enddocregion Foo-Object-error
}
}
4 changes: 2 additions & 2 deletions examples/misc/lib/language_tour/libraries/import_as.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'lib1.dart';
import 'lib2.dart' as lib2;

// Uses Element from lib1.
Element element1 = new Element();
Element element1 = Element();

// Uses Element from lib2.
lib2.Element element2 = new lib2.Element();
lib2.Element element2 = lib2.Element();
2 changes: 1 addition & 1 deletion examples/misc/lib/language_tour/libraries/show_hide.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import 'lib1.dart' show foo;
import 'lib2.dart' hide foo;
// #enddocregion

Element e = new Element();
Element e = Element();
dynamic bar = foo;
6 changes: 3 additions & 3 deletions examples/misc/lib/language_tour/operators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ void miscDeclAnalyzedButNotTested() {

{
// #docregion nested-cascades
final addressBook = (new AddressBookBuilder()
final addressBook = (AddressBookBuilder()
..name = 'jenny'
..email = '[email protected]'
..phone = (new PhoneNumberBuilder()
..phone = (PhoneNumberBuilder()
..number = '415-555-0100'
..label = 'home')
.build())
Expand All @@ -31,7 +31,7 @@ void miscDeclAnalyzedButNotTested() {

{
// #docregion cannot-cascade-on-void
var sb = new StringBuffer();
var sb = StringBuffer();
sb.write('foo')
// #enddocregion cannot-cascade-on-void
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SortedCollection {
int sort(Object a, Object b) => 0;

void main() {
SortedCollection coll = new SortedCollection(sort);
SortedCollection coll = SortedCollection(sort);

// All we know is that compare is a function,
// but what type of function?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SortedCollection {
int sort(Object a, Object b) => 0;

void main() {
SortedCollection coll = new SortedCollection(sort);
SortedCollection coll = SortedCollection(sort);
assert(coll.compare is Function);
assert(coll.compare is Compare);
}
Loading