Use switch expressions and pattern matching where appropriate
63
75%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
View guide
Passed
No findings from the security scan
Fix and improve this skill with Tessl
tessl review fix ./skills/dart-use-pattern-matching/SKILL.mdApply specific pattern types based on the data structure and desired outcome. Follow these conditional guidelines:
sealed classes to ensure exhaustiveness.>=, <=) and Logical-and (&&) patterns.||) patterns to share a single case body or guard clause._) or a non-matching Rest element (...) in collections.Select the appropriate switch construct based on the execution context:
switch (value) { pattern => expression, }switch (value) { case pattern: statements; }break keyword required).Implement patterns using the following syntax and rules:
||): pattern1 || pattern2. Both branches must define the exact same set of variables.&&): pattern1 && pattern2. Branches must not define overlapping variables.==, !=, <, >, <=, >= followed by a constant expression.as): pattern as Type. Throws if the value does not match the type. Use to forcibly assert types during destructuring.?): pattern?. Fails the match if the value is null. Binds the variable to the non-nullable base type.!): pattern!. Throws if the value is null.var name or Type name. Binds the matched value to a new local variable._): Matches any value and discards it.[pattern1, pattern2]. Matches lists of exact length unless a Rest element (... or ...var rest) is used.{"key": pattern}. Matches maps containing the specified keys. Ignores unmatched keys.(pattern1, named: pattern2). Matches records of the exact shape. Use :var name to infer the getter name.ClassName(field: pattern). Matches instances of ClassName. Use :var field to infer the getter name.Copy this checklist to track progress when implementing complex pattern matching logic:
var x, :var y).when condition) for logic that cannot be expressed via patterns._) or default clause (if not using a sealed class).When switching over sealed classes or enums, you must ensure all subtypes are handled.
dart analyze._) case if a default fallback is acceptable.Use Map and List patterns to validate structure and extract data in a single step.
Input:
var data = {
'user': ['Lily', 13],
};Implementation:
if (data case {'user': [String name, int age]}) {
print('User $name is $age years old.');
} else {
print('Invalid JSON structure.');
}Use Object patterns with switch expressions to handle family types exhaustively.
Implementation:
sealed class Shape {}
class Square implements Shape {
final double length;
Square(this.length);
}
class Circle implements Shape {
final double radius;
Circle(this.radius);
}
// Switch expression guarantees exhaustiveness due to `sealed` modifier.
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l) => l * l,
Circle(:var radius) => math.pi * radius * radius,
};Use variable assignment patterns to swap values or extract record fields without temporary variables.
Implementation:
var (a, b) = ('left', 'right');
(b, a) = (a, b); // Swap values
// Destructuring a function return
var (name, age) = getUserInfo();Use when to evaluate arbitrary conditions after a pattern matches.
Implementation:
switch (shape) {
case Square(size: var s) || Circle(size: var s) when s > 0:
print('Valid symmetric shape with size $s');
case Square() || Circle():
print('Invalid or empty shape');
default:
print('Unknown shape');
}8aaa41d
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.