Function sway_core::parse_tree::desugar_match_expression
source · [−]pub fn desugar_match_expression(
primary_expression: Expression,
branches: Vec<MatchBranch>,
_span: Span
) -> CompileResult<Expression>
Expand description
This algorithm desugars match expressions to if statements.
Given the following example:
ⓘ
struct Point {
x: u64,
y: u64
}
let p = Point {
x: 42,
y: 24
};
match p {
Point { x, y: 5 } => { x },
Point { x, y: 24 } => { x },
_ => 0
}
The resulting if statement would look roughly like this:
ⓘ
if y==5 {
let x = 42;
x
} else if y==42 {
let x = 42;
x
} else {
0
}
The steps of the algorithm can roughly be broken down into:
- Assemble the “matched branches.”
- Assemble the possibly nested giant if statement using the matched branches. 2a. Assemble the conditional that goes in the if primary expression. 2b. Assemble the statements that go inside of the body of the if expression 2c. Assemble the giant if statement.
- Return!