Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 Oct 14, 2025
9632d2c
add changes to dot shorthands
connie-ooi Oct 22, 2025
9d4555e
fix title for side nav
connie-ooi Oct 22, 2025
ef0458b
fix experimental tag
connie-ooi Oct 22, 2025
54f4900
update dotshorthands page
connie-ooi Oct 24, 2025
f13852c
fix dotshorthand code comments
connie-ooi Oct 25, 2025
7a0af1b
Fix permalink for Dot Shorthands in sidenav.yml
connie-ooi Oct 27, 2025
626a58e
Rename dot-shorthand.md to dot-shorthands.md
connie-ooi Oct 27, 2025
362c0a2
Fix URL typo in extension-methods.md
connie-ooi Oct 27, 2025
2fb223a
Fix typo in next page URL for enums.md
connie-ooi Oct 27, 2025
d9a9e82
Fix link to dot shorthand syntax documentation
connie-ooi Oct 27, 2025
bc01351
Revise title and description in dot-shorthands.md
connie-ooi Oct 27, 2025
83efd2d
Update src/data/sidenav.yml
connie-ooi Oct 29, 2025
ded2a61
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
f3852f5
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
7f3f381
Update examples/language/lib/classes/shorthand.dart
connie-ooi Oct 29, 2025
575a975
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
fe0a105
Update src/content/language/extension-methods.md
connie-ooi Oct 29, 2025
2345e51
Update src/content/language/enums.md
connie-ooi Oct 29, 2025
03bc858
Update usage section
connie-ooi Oct 29, 2025
9ba3572
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
25c1786
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
a321f11
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
860442c
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
2ef50c2
Update src/content/language/dot-shorthands.md
connie-ooi Oct 29, 2025
9116923
update dotshorthands
connie-ooi Oct 31, 2025
989f2a6
update to code files and content
connie-ooi Nov 3, 2025
f3889af
fix formatting issues
connie-ooi Nov 3, 2025
36fc494
Get code excerpts passing analysis and run formatter
parlough Nov 4, 2025
c6dc1dc
Rename excerpts directory to shorthands
parlough Nov 4, 2025
fdb192d
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
4c5034e
Update src/content/language/index.md
connie-ooi Nov 4, 2025
d56799b
Update src/content/language/index.md
connie-ooi Nov 4, 2025
ced0b59
Update src/content/language/index.md
connie-ooi Nov 4, 2025
f4f6c42
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
7979a76
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
b91130a
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
60d1c9a
Update src/content/language/index.md
connie-ooi Nov 4, 2025
df0ed1f
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
b523779
Update src/content/language/index.md
connie-ooi Nov 4, 2025
9f22206
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
38bec1c
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
22ef698
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
e135f2d
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
bc12888
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
6daacac
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
23ec086
Update examples/language/lib/shorthands/equality_with_errors.dart
connie-ooi Nov 4, 2025
ded9527
Update examples/language/lib/shorthands/enums.dart
connie-ooi Nov 4, 2025
f015647
Update examples/language/lib/shorthands/enums.dart
connie-ooi Nov 4, 2025
7608108
Update examples/language/lib/shorthands/intro.dart
connie-ooi Nov 4, 2025
d2c856f
Update examples/language/lib/shorthands/intro.dart
connie-ooi Nov 4, 2025
fe20bee
Update examples/language/lib/shorthands/intro.dart
connie-ooi Nov 4, 2025
621928a
Fix comments and edits
connie-ooi Nov 4, 2025
6edbaca
Fix analysis and formatting
parlough Nov 4, 2025
be4159f
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
6cd9bc6
Update src/content/language/dot-shorthands.md
connie-ooi Nov 4, 2025
ac400d7
Merge branch 'main' into dotshorthands
connie-ooi Nov 4, 2025
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
10 changes: 10 additions & 0 deletions examples/language/lib/shorthands/chain.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
void printLowerH() {
// #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
}
19 changes: 19 additions & 0 deletions examples/language/lib/shorthands/constructors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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]);
}
}

// Use dot shorthand syntax on a named constructor:
Point origin = .origin(); // Instead of Point.origin()

// Use dot shorthand syntax on a factory constructor:
Point p1 = .fromList([1.0, 2.0]); // Instead of Point.fromList([1.0, 2.0])

// Use dot shorthand syntax on a generic class constructor:
List<int> intList = .filled(5, 0); // Instead of List.filled(5, 0)
17 changes: 17 additions & 0 deletions examples/language/lib/shorthands/consts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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;
}

// Use dot shorthand syntax for enum value:
const Status defaultStatus = .running; // Instead of Status.running

// Use dot shorthand syntax to invoke a const named constructor:
const Point myOrigin = .origin(); // Instead of Point.origin()

// Use dot shorthand syntax in a const collection literal:
const List<Point> keyPoints = [.origin(), .new(1.0, 1.0)];
// Instead of [Point.origin(), Point(1.0, 1.0)]
15 changes: 15 additions & 0 deletions examples/language/lib/shorthands/enums.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enum LogLevel { debug, info, warning, error }

/// Returns the color code to use for the specified log [level].
String colorCode(LogLevel level) {
// Use dot shorthand syntax for enum values in switch cases:
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:
String warnColor = colorCode(.warning); // Returns 'orange'
23 changes: 23 additions & 0 deletions examples/language/lib/shorthands/equality.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ignore_for_file: dead_code
enum Color { red, green, blue }

// Use dot shorthand syntax for equality expressions:
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');
}
28 changes: 28 additions & 0 deletions examples/language/lib/shorthands/equality_with_errors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ignore_for_file: dead_code
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.
// ignore: dot_shorthand_missing_context
if (.red == myColor) {
print('This will not compile.');
}

// ERROR: The right-hand side is a complex expression (a conditional expression),
// which is not a valid target for shorthand in a comparison.
// ignore: dot_shorthand_missing_context
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`.
// ignore: dot_shorthand_undefined_member
if ((myColor as Object) == .green) {
print('This will not compile.');
}
}
13 changes: 13 additions & 0 deletions examples/language/lib/shorthands/expression_with_errors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Logger {
static void log(String message) {
print(message);
}
}

void main() {
// ERROR: An expression statement can't begin with `.`.
// The compiler has no type context (like a variable assignment)
// to infer that `.log` should refer to `Logger.log`.
// ignore: dot_shorthand_undefined_member
.log('Hello');
}
16 changes: 16 additions & 0 deletions examples/language/lib/shorthands/intro.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Use dot shorthand syntax on enums:
enum Status { none, running, stopped, paused }

Status currentStatus = .running; // Instead of Status.running

// Use dot shorthand syntax on a static method:
int port = .parse('8080'); // Instead of int.parse('8080')

// Uses dot shorthand syntax on a constructor:
class Point {
final int x, y;
Point(this.x, this.y);
Point.origin() : x = 0, y = 0;
}

Point origin = .origin(); // Instead of Point.origin()
5 changes: 5 additions & 0 deletions examples/language/lib/shorthands/static_members.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Use dot shorthand syntax to invoke a static method:
int httpPort = .parse('80'); // Instead of int.parse('80')

// Use dot shorthand syntax to access a static field or getter:
BigInt bigIntZero = .zero; // Instead of BigInt.zero
43 changes: 43 additions & 0 deletions examples/language/lib/shorthands/unnamed_constructors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ignore_for_file: unused_field, unused_element

// #docregion unnamed-before
class _PageState extends State<Page> {
late 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 unnamed-before

// #docregion unnamed-after
// Use dot shorthand syntax for calling unnamed constructors:
class _PageStateAfter extends State<Page> {
late final AnimationController _animationController = .new(vsync: this);
final ScrollController _scrollController = .new();
final GlobalKey<ScaffoldMessengerState> scaffoldKey = .new();
Map<String, Map<String, bool>> properties = .new();
// ...
}
// #enddocregion unnamed-after

// Supporting classes for example.

class State<T> {}

class ScaffoldMessengerState {}

class GlobalKey<T> {}

class ScrollController {}

class AnimationController {
AnimationController({State<Object>? vsync});
}

class Page {}
2 changes: 1 addition & 1 deletion examples/language/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: dart.dev example code.

resolution: workspace
environment:
sdk: ^3.9.0
sdk: ^3.10.0-0

dependencies:
charcode: ^1.4.0
Expand Down
Loading
Loading