Skip to content

Commit

Permalink
Merge pull request #5657 from poorna2152/binding_patterns
Browse files Browse the repository at this point in the history
Update binding patterns BBEs
  • Loading branch information
gimantha authored Sep 23, 2024
2 parents 5e797d6 + 08c4b6f commit a7813da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ function matchCommand(any commands) {
var [show] => {
io:println(show);
}
// The list binding pattern below binds lists that contain three list items
// where the third element in the list is the boolean value `true`.
var [remove, all, isDir] if isDir is true => {
io:println(remove, " directories ", all);
}
// The list binding pattern below binds lists that contain three list items.
var [remove, all, _] => {
io:println(remove, " ", all);
io:println(remove, " files ", all);
}
// The list binding pattern below binds lists that contain two list items,
// in which the second list item is also a list of two items.
Expand All @@ -20,9 +25,11 @@ function matchCommand(any commands) {
}
}


public function main() {
matchCommand(["Show"]);
matchCommand(["Remove", "*", true]);
matchCommand(["Remove", "f.txt", false]);
matchCommand(["Copy", ["a.bal", "b.bal"]]);
matchCommand(1);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$ bal run list_binding_pattern_in_match_statement.bal
Show
Remove *
Remove directories *
Remove files f.txt
Copy a.bal into b.bal
Unrecognized command.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,30 @@ function matchTime3(Day day) {
}
}

function matchTime4(Time time) {
match time {
// The binding pattern below has a rest binding pattern to capture the additional
// fields that may be specified in the open record value assigned to the `time` variable.
// The condition here checks whether the open record has the field `meridiem`.
var {hours, minutes, ...rest}
if rest["meridiem"] !is () && rest["meridiem"] == "PM" => {
io:println(hours + 12, ", ", minutes);
}
_ => {
io:println(time.hours, ", ", time.minutes);
}
}
}

public function main() {
Time time = {hours: 3, minutes: 20, "seconds": 40, "milli-seconds": 500};
matchTime1(time);
matchTime2(time);

Day day = {t: time};
matchTime3(day);

Time time2 = {hours: 11, minutes: 21, "seconds": 52, "meridiem": "PM"};
matchTime4(time2);
matchTime4(time);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ $ bal run mapping_binding_pattern_in_match_statement.bal
3, 20
3, 20, {"seconds":40,"milli-seconds":500}
3, 20
23, 21
3, 20

0 comments on commit a7813da

Please sign in to comment.