-
Notifications
You must be signed in to change notification settings - Fork 744
[Dart 3.10] Document dot shorthands #6941
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
082e043
add initial draft of dot shorthands feature
connie-ooi 9632d2c
add changes to dot shorthands
connie-ooi 9d4555e
fix title for side nav
connie-ooi ef0458b
fix experimental tag
connie-ooi 54f4900
update dotshorthands page
connie-ooi f13852c
fix dotshorthand code comments
connie-ooi 7a0af1b
Fix permalink for Dot Shorthands in sidenav.yml
connie-ooi 626a58e
Rename dot-shorthand.md to dot-shorthands.md
connie-ooi 362c0a2
Fix URL typo in extension-methods.md
connie-ooi 2fb223a
Fix typo in next page URL for enums.md
connie-ooi d9a9e82
Fix link to dot shorthand syntax documentation
connie-ooi bc01351
Revise title and description in dot-shorthands.md
connie-ooi 83efd2d
Update src/data/sidenav.yml
connie-ooi ded2a61
Update src/content/language/dot-shorthands.md
connie-ooi f3852f5
Update src/content/language/dot-shorthands.md
connie-ooi 7f3f381
Update examples/language/lib/classes/shorthand.dart
connie-ooi 575a975
Update src/content/language/dot-shorthands.md
connie-ooi fe0a105
Update src/content/language/extension-methods.md
connie-ooi 2345e51
Update src/content/language/enums.md
connie-ooi 03bc858
Update usage section
connie-ooi 9ba3572
Update src/content/language/dot-shorthands.md
connie-ooi 25c1786
Update src/content/language/dot-shorthands.md
connie-ooi a321f11
Update src/content/language/dot-shorthands.md
connie-ooi 860442c
Update src/content/language/dot-shorthands.md
connie-ooi 2ef50c2
Update src/content/language/dot-shorthands.md
connie-ooi 9116923
update dotshorthands
connie-ooi 989f2a6
update to code files and content
connie-ooi f3889af
fix formatting issues
connie-ooi 36fc494
Get code excerpts passing analysis and run formatter
parlough c6dc1dc
Rename excerpts directory to shorthands
parlough fdb192d
Update src/content/language/dot-shorthands.md
connie-ooi 4c5034e
Update src/content/language/index.md
connie-ooi d56799b
Update src/content/language/index.md
connie-ooi ced0b59
Update src/content/language/index.md
connie-ooi f4f6c42
Update src/content/language/dot-shorthands.md
connie-ooi 7979a76
Update src/content/language/dot-shorthands.md
connie-ooi b91130a
Update src/content/language/dot-shorthands.md
connie-ooi 60d1c9a
Update src/content/language/index.md
connie-ooi df0ed1f
Update src/content/language/dot-shorthands.md
connie-ooi b523779
Update src/content/language/index.md
connie-ooi 9f22206
Update src/content/language/dot-shorthands.md
connie-ooi 38bec1c
Update src/content/language/dot-shorthands.md
connie-ooi 22ef698
Update src/content/language/dot-shorthands.md
connie-ooi e135f2d
Update src/content/language/dot-shorthands.md
connie-ooi bc12888
Update src/content/language/dot-shorthands.md
connie-ooi 6daacac
Update src/content/language/dot-shorthands.md
connie-ooi 23ec086
Update examples/language/lib/shorthands/equality_with_errors.dart
connie-ooi ded9527
Update examples/language/lib/shorthands/enums.dart
connie-ooi f015647
Update examples/language/lib/shorthands/enums.dart
connie-ooi 7608108
Update examples/language/lib/shorthands/intro.dart
connie-ooi d2c856f
Update examples/language/lib/shorthands/intro.dart
connie-ooi fe20bee
Update examples/language/lib/shorthands/intro.dart
connie-ooi 621928a
Fix comments and edits
connie-ooi 6edbaca
Fix analysis and formatting
parlough be4159f
Update src/content/language/dot-shorthands.md
connie-ooi 6cd9bc6
Update src/content/language/dot-shorthands.md
connie-ooi ac400d7
Merge branch 'main' into dotshorthands
connie-ooi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // #docregion intro | ||
| // Enum example | ||
| enum Status { none, running, stopped, paused } | ||
| Status currentStatus = .running; // Instead of Status.running | ||
|
|
||
| // Static method example | ||
| int port = .parse('8080'); // Instead of int.parse('8080') | ||
|
|
||
| // Constructor example | ||
| class Point { | ||
| final int x, y; | ||
| Point(this.x, this.y); | ||
| Point.origin() : x = 0, y = 0; | ||
| } | ||
| Point origin = .origin(); // Instead of Point.origin() | ||
| // #enddocregion intro | ||
|
|
||
| // #docregion enums | ||
| // Enum example | ||
| enum LogLevel { debug, info, warning, error } | ||
|
|
||
| // Function to get a color code based on log level | ||
| String colorCode(LogLevel level) { | ||
| return switch (level) { | ||
| .debug => 'gray', // Instead of LogLevel.debug | ||
| .info => 'blue', // Instead of LogLevel.info | ||
| .warning => 'orange', // Instead of LogLevel.warning | ||
| .error => 'red', // Instead of LogLevel.error | ||
| }; | ||
| } | ||
|
|
||
| // Example usage: | ||
| var warnColor = colorCode(.warning); // Returns 'orange' | ||
|
|
||
| // #enddocregion enums | ||
|
|
||
| // #docregion methods | ||
| // Static method | ||
| int httpPort = .parse('80'); // Instead of int.parse('80') | ||
|
|
||
| // Static field/getter | ||
| BigInt bigIntZero = .zero; // Instead of BigInt.zero | ||
| // #enddocregion methods | ||
|
|
||
| // #docregion constructors | ||
| class Point { | ||
| final double x, y; | ||
| const Point(this.x, this.y); | ||
| const Point.origin() : x = 0, y = 0; // Named constructor | ||
|
|
||
| // Factory constructor | ||
| factory Point.fromList(List<double> list) { | ||
| return Point(list[0], list[1]); | ||
| } | ||
| } | ||
|
|
||
| // Named constructor | ||
connie-ooi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Point origin = .origin(); // Instead of Point.origin() | ||
|
|
||
| // Factory constructor | ||
| Point p1 = .fromList([1.0, 2.0]); // Instead of Point.fromList([1.0, 2.0]) | ||
|
|
||
| // Generic class constructor | ||
| List<int> intList = .filled(5, 0); // Instead of List.filled(5, 0) | ||
| // #enddocregion constructors | ||
|
|
||
| // #docregion tearoff | ||
|
|
||
| // #enddocregion tearoff | ||
|
|
||
| // #docregion chain | ||
| // .fromCharCode(72) resolves to the String "H", | ||
| // then the instance method .toLowerCase() is called on that String. | ||
| String lowerH = .fromCharCode(72).toLowerCase(); | ||
| // Instead of String.fromCharCode(72).toLowerCase() | ||
|
|
||
| print(lowerH); // Output: h | ||
| // #enddocregion chain | ||
|
|
||
| // #docregion allowedequality | ||
connie-ooi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| enum Color { red, green, blue } | ||
|
|
||
| void allowedExamples() { | ||
| Color myColor = Color.red; | ||
| bool condition = true; | ||
|
|
||
| // OK: `myColor` is a `Color`, so `.green` is inferred as `Color.green`. | ||
| if (myColor == .green) { | ||
| print('The color is green.'); | ||
| } | ||
|
|
||
| // OK: Works with `!=` as well. | ||
| if (myColor != .blue) { | ||
| print('The color is not blue.'); | ||
| } | ||
|
|
||
| // OK: The context for the ternary is the variable `inferredColor` | ||
| // being assigned to, which has a type of `Color`. | ||
| Color inferredColor = condition ? .green : .blue; | ||
| print('Inferred color is $inferredColor'); | ||
| } | ||
| // #enddocregion allowedequality | ||
|
|
||
| // #docregion notallowedequality | ||
| enum Color { red, green, blue } | ||
|
|
||
| void notAllowedExamples() { | ||
| Color myColor = Color.red; | ||
| bool condition = true; | ||
|
|
||
| // ERROR: The shorthand must be on the right side of `==`. | ||
| // Dart's `==` operator is not symmetric for this feature. | ||
| if (.red == myColor) { | ||
| print('This will not compile.'); | ||
| } | ||
|
|
||
| // ERROR: The right-hand side is a complex expression (a ternary), | ||
| // which is not a valid target for shorthand in a comparison. | ||
| if (myColor == (condition ? .green : .blue)) { | ||
| print('This will not compile.'); | ||
| } | ||
|
|
||
| // ERROR: The type context is lost by casting `myColor` to `Object`. | ||
| // The compiler no longer knows that `.green` should refer to `Color.green`. | ||
| if ((myColor as Object) == .green) { | ||
| print('This will not compile.'); | ||
| } | ||
| } | ||
| // #enddocregion notallowedequality | ||
|
|
||
| // #docregion const | ||
| enum Status { none, running, stopped, paused } | ||
| class Point { | ||
| final double x, y; | ||
| const Point(this.x, this.y); | ||
| const Point.origin() : x = 0.0, y = 0.0; | ||
| } | ||
|
|
||
| // Enum values are always constants | ||
| const Status defaultStatus = .running; // Instead of Status.running | ||
|
|
||
| // Invoking a const named constructor | ||
| const Point myOrigin = .origin(); // Instead of Point.origin() | ||
|
|
||
| // Using shorthands in a const collection literal | ||
| const List<Point> keyPoints = [ .origin(), .new(1.0, 1.0) ]; | ||
| // Instead of [Point.origin(), Point(1.0, 1.0)] | ||
| // #enddocregion const | ||
|
|
||
| // #docregion unnamedbefore | ||
| class _PageState extends State<Page> { | ||
| final AnimationController _animationController = AnimationController(vsync: this); | ||
| final ScrollController _scrollController = ScrollController(); | ||
|
|
||
| final GlobalKey<ScaffoldMessengerState> scaffoldKey = | ||
| GlobalKey<ScaffoldMessengerState>(); | ||
|
|
||
| Map<String, Map<String, bool>> properties | ||
| = <String, Map<String, bool>>{}; | ||
| // ... | ||
| } | ||
| // #enddocregion unnamedbefore | ||
|
|
||
| // #docregion unnamedafter | ||
| class _PageState extends State<Page> { | ||
| final AnimationController _animationController = .new(vsync: this); | ||
| final ScrollController _scrollController = .new(); | ||
| final GlobalKey<ScaffoldMessengerState> scaffoldKey = .new(); | ||
| Map<String, Map<String, bool>> properties = .new(); | ||
| // ... | ||
| } | ||
| // #enddocregion unnamedafter | ||
connie-ooi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.