-
Couldn't load subscription status.
- Fork 740
[Dart 3.10] dot shorthands feature #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
Draft
connie-ooi
wants to merge
12
commits into
dart-lang:main
Choose a base branch
from
connie-ooi:dotshorthands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+468
−4
Draft
Changes from all commits
Commits
Show all changes
12 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 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 | ||
| 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 | ||
| 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 | ||
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.