Skip to content

Commit

Permalink
adds _ to macro lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Jan 23, 2025
1 parent 7fa4c69 commit 9effb52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/sui-framework/packages/move-stdlib/sources/macros.move
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public macro fun num_to_string($x: _): String {
buffer.to_string()
}

public macro fun range_do($start: _, $stop: _, $f: |_|) {
public macro fun range_do($start: _, $stop: _, $f: |_| -> _) {
let mut i = $start;
let stop = $stop;
while (i < stop) {
Expand All @@ -93,7 +93,7 @@ public macro fun range_do($start: _, $stop: _, $f: |_|) {
}
}

public macro fun range_do_eq($start: _, $stop: _, $f: |_|) {
public macro fun range_do_eq($start: _, $stop: _, $f: |_| -> _) {
let mut i = $start;
let stop = $stop;
// we check `i >= stop` inside the loop instead of `i <= stop` as `while` condition to avoid
Expand All @@ -108,11 +108,11 @@ public macro fun range_do_eq($start: _, $stop: _, $f: |_|) {
}
}

public macro fun do($stop: _, $f: |_|) {
public macro fun do($stop: _, $f: |_| -> _) {
range_do!(0, $stop, $f)
}

public macro fun do_eq($stop: _, $f: |_|) {
public macro fun do_eq($stop: _, $f: |_| -> _) {
range_do_eq!(0, $stop, $f)
}

Expand Down
16 changes: 16 additions & 0 deletions crates/sui-framework/packages/move-stdlib/tests/vector_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,11 @@ fun test_do_macro() {
assert!(vec == vector[11, 21, 31, 41]);
}

#[test]
fun test_do_macro_with_return_value() {
vector[10, 20, 30].do!(|e| e);
}

#[test]
fun test_map_macro() {
let e = vector<u8>[];
Expand Down Expand Up @@ -741,6 +746,17 @@ fun zip_do_macro() {
assert!(res == vector[5, 7, 9]);
}

#[test]
fun zip_do_undroppable_macro() {
let v1 = vector[NotDroppable {}, NotDroppable {}];
let v2 = vector[NotDroppable {}, NotDroppable {}];

v1.zip_do!(v2, |a, b| {
let NotDroppable {} = a;
let NotDroppable {} = b;
});
}

#[test, expected_failure]
fun zip_do_reverse_macro_fail() {
let v1 = vector[1u64];
Expand Down

0 comments on commit 9effb52

Please sign in to comment.