Skip to content
Merged
Show file tree
Hide file tree
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 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
172 changes: 172 additions & 0 deletions examples/language/lib/classes/shorthand.dart
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
Loading
Loading