forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sway Ref: Style guide pattern matching (FuelLabs#4655)
- Loading branch information
Showing
6 changed files
with
120 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
2 changes: 2 additions & 0 deletions
2
docs/reference/src/code/language/style-guide/pattern_matching/.gitignore
This file contains 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,2 @@ | ||
out | ||
target |
7 changes: 7 additions & 0 deletions
7
docs/reference/src/code/language/style-guide/pattern_matching/Forc.toml
This file contains 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,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "pattern_matching" | ||
|
||
[dependencies] |
34 changes: 34 additions & 0 deletions
34
docs/reference/src/code/language/style-guide/pattern_matching/src/lib.sw
This file contains 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,34 @@ | ||
library; | ||
|
||
#[allow(dead_code)] | ||
enum Shape { | ||
Triangle: (), | ||
Quadrilateral: (), | ||
Pentagon: (), | ||
Hexagon: (), | ||
Heptagon: (), | ||
} | ||
|
||
#[allow(dead_code)] | ||
// ANCHOR: style_match_unnamed | ||
fn unnamed_case(shape: Shape) { | ||
let value = match shape { | ||
Shape::Triangle => 3, | ||
Shape::Quadrilateral => 4, | ||
Shape::Pentagon => 5, | ||
_ => 0, | ||
}; | ||
} | ||
// ANCHOR_END: style_match_unnamed | ||
|
||
#[allow(dead_code)] | ||
// ANCHOR: style_match_named | ||
fn named_case(shape: Shape) { | ||
let value = match shape { | ||
Shape::Triangle => 3, | ||
Shape::Quadrilateral => 4, | ||
Shape::Pentagon => 5, | ||
_invalid_shape => 0, | ||
}; | ||
} | ||
// ANCHOR_END: style_match_named |
18 changes: 17 additions & 1 deletion
18
docs/reference/src/documentation/language/style-guide/pattern-matching.md
This file contains 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 |
---|---|---|
@@ -1,3 +1,19 @@ | ||
# Pattern Matching | ||
|
||
> TODO: throw away variables `_` and catch all case names | ||
The following examples present pattern matching using the [`match`](../../language/control-flow/match/index.md) keyword for the catch-all case. | ||
|
||
## Encouraged | ||
|
||
The `_` is used for the catch-all to indicate the important cases have been defined above and the last case is not important enough to warrant a name. | ||
|
||
```sway | ||
{{#include ../../../code/language/style-guide/pattern_matching/src/lib.sw:style_match_unnamed}} | ||
``` | ||
|
||
## Alternative | ||
|
||
We may apply an appropriate name to provide context to the reader; however, unless it provides additional information the preferred usage is defined in the [`encouraged`](#encouraged) case. | ||
|
||
```sway | ||
{{#include ../../../code/language/style-guide/pattern_matching/src/lib.sw:style_match_named}} | ||
``` |