Skip to content

Commit 9632d2c

Browse files
committed
add changes to dot shorthands
1 parent 082e043 commit 9632d2c

File tree

5 files changed

+216
-130
lines changed

5 files changed

+216
-130
lines changed

examples/language/lib/classes/shorthand.dart

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ String colorCode(LogLevel level) {
3030
}
3131

3232
// Example usage:
33-
// var warnColor = colorCode(.warning); // Returns 'orange'
33+
var warnColor = colorCode(.warning); // Returns 'orange'
3434

3535
// #enddocregion enums
3636

@@ -57,43 +57,19 @@ Point origin = .origin(); // Instead of Point.origin()
5757
Point p1 = .fromList([1.0, 2.0]); // Instead of Point.fromList([1.0, 2.0])
5858

5959
// With type arguments for generic class constructors
60-
List<int> intList = .filled(5, 0); // Instead of List<int>.filled(5, 0)
60+
List<int> intList = .filled(5, 0); // Instead of List.filled(5, 0)
6161
// #enddocregion constructors
6262

6363
// #docregion tearoff
64-
class Logger {
65-
final String name;
66-
Logger(this.name); // Unnamed constructor
67-
Logger.verbose() : name = 'VERBOSE'; // Named constructor
68-
}
69-
70-
void main() {
71-
// .new for an UNNAMED constructor tear-off:
72-
Logger Function(String) createLogger = .new; // Instead of Logger.new
73-
74-
// .identifier for a NAMED constructor tear-off:
75-
Logger Function() createVerboseLogger = .verbose; // Instead of Logger.verbose
76-
77-
// .new for a generic UNNAMED constructor tear-off:
78-
Set<String> Function() createStringSet = .new; // Instead of Set<String>.new
79-
80-
// Now use the tear-offs to create instances:
81-
var appLog = createLogger('App');
82-
var verboseLog = createVerboseLogger();
83-
var stringSet = createStringSet();
8464

85-
print(appLog.name); // Prints "App"
86-
print(verboseLog.name); // Prints "VERBOSE"
87-
print(stringSet.isEmpty); // Prints "true"
88-
}
8965
// #enddocregion tearoff
9066

9167
// #docregion chain
9268
// .fromCharCode(72) resolves to the String "H",
9369
// then the instance method .toLowerCase() is called on that String.
9470
String lowerH = .fromCharCode(72).toLowerCase(); // Instead of String.fromCharCode(72).toLowerCase()
9571

96-
// print(lowerH); // Output: h
72+
print(lowerH); // Output: h
9773
// #enddocregion chain
9874

9975
// #docregion allowedequality
@@ -165,21 +141,83 @@ const Point myOrigin = .origin(); // Instead of const Point.origin()
165141
const List<Point> keyPoints = [ .origin(), .new(1.0, 1.0) ]; // Instead of [const Point.origin(), const Point(1.0, 1.0)]
166142
// #enddocregion const
167143

168-
// #docregion nested
169-
// AVOID: Hard to read, types are hidden
170-
// Widget complex = .container(
171-
// child: .padding(
172-
// padding: .all(8.0),
173-
// child: .text('Hello')
174-
// )
175-
// );
176-
177-
// PREFER: Explicit types in nested structures
178-
Widget complex = Container( // Assuming types
179-
child: Padding(
180-
padding: EdgeInsets.all(8.0),
181-
child: Text('Hello')
182-
)
183-
);
184-
185-
// #enddocregion nested
144+
// #docregion best
145+
// GOOD: Use dot shorthands in typed collections.
146+
final alignments = <MainAxisAlignment>[.center, .bottomLeft];
147+
148+
List<Person>[
149+
.new(name: 'Joe', age: 145),
150+
.new(name: 'Alice', age: 495),
151+
];
152+
153+
// GOOD: Use dot shorthands for implicit return values.
154+
class Foo {
155+
MainAxisAlignment pickAlignment() => .start;
156+
EdgeInsets get padding => .all(8.0);
157+
}
158+
159+
160+
161+
// #enddocregion best
162+
163+
// #docregion avoid
164+
Size calculateSize() {
165+
// AVOID: Return statement type is not obvious from the return statement alone.
166+
return .fromHeight(10);
167+
}
168+
169+
Size calculateSize() {
170+
// GOOD: Return statement type is obvious.
171+
return Size.fromHeight(10);
172+
}
173+
174+
175+
// AVOID: The type of _character isn't obvious in this context.
176+
setState(() {
177+
_character = .jefferson;
178+
});
179+
180+
// AVOID: Prefer using explicit types for arrow syntax.
181+
ScrollController buildController() => .new();
182+
183+
184+
GlobalKey<ScaffoldMessengerState> buildKey() {
185+
// AVOID: Don't use .new() as a shorthand in return statements.
186+
return .new();
187+
188+
// GOOD
189+
return GlobalKey<ScaffoldMessengerState>();
190+
}
191+
192+
// AVOID: Don't use .new() for class field declarations
193+
class Foo {
194+
// BAD: Omit obvious types instead of using .new.
195+
final ScrollController _controller = .new();
196+
197+
// GOOD
198+
final _controller = ScrollController();
199+
}
200+
201+
// #enddocregion avoid
202+
203+
// #docregion unnamed
204+
class _PageState extends State<Page> {
205+
// Before
206+
final AnimationController _animationController = AnimationController(vsync: this);
207+
final ScrollController _scrollController = ScrollController();
208+
209+
final GlobalKey<ScaffoldMessengerState> scaffoldKey =
210+
GlobalKey<ScaffoldMessengerState>();
211+
212+
Map<String, Map<String, bool>> properties
213+
= <String, Map<String, bool>>{};
214+
215+
// After
216+
final AnimationController _animationController = .new(vsync: this);
217+
final ScrollController _scrollController = .new();
218+
final GlobalKey<ScaffoldMessengerState> scaffoldKey = .new();
219+
Map<String, Map<String, bool>> properties = .new();
220+
221+
// ...
222+
}
223+
// #enddocregion unnamed

0 commit comments

Comments
 (0)