-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add syntax for specifying function type environments (#2357)
- Loading branch information
1 parent
36fe1ee
commit 495a479
Showing
9 changed files
with
158 additions
and
80 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/nargo_cli/tests/execution_success/closure_explicit_types/Nargo.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 @@ | ||
[package] | ||
name = "closure_explicit_types" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = "0.10.3" | ||
|
||
[dependencies] |
60 changes: 60 additions & 0 deletions
60
crates/nargo_cli/tests/execution_success/closure_explicit_types/src/main.nr
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,60 @@ | ||
|
||
fn ret_normal_lambda1() -> fn() -> Field { | ||
|| 10 | ||
} | ||
|
||
// explicitly specified empty capture group | ||
fn ret_normal_lambda2() -> fn[]() -> Field { | ||
|| 20 | ||
} | ||
|
||
// return lamda that captures a thing | ||
fn ret_closure1() -> fn[Field]() -> Field { | ||
let x = 20; | ||
|| x + 10 | ||
} | ||
|
||
// return lamda that captures two things | ||
fn ret_closure2() -> fn[Field,Field]() -> Field { | ||
let x = 20; | ||
let y = 10; | ||
|| x + y + 10 | ||
} | ||
|
||
// return lamda that captures two things with different types | ||
fn ret_closure3() -> fn[u32,u64]() -> u64 { | ||
let x: u32 = 20; | ||
let y: u64 = 10; | ||
|| x as u64 + y + 10 | ||
} | ||
|
||
// accepts closure that has 1 thing in its env, calls it and returns the result | ||
fn accepts_closure1(f: fn[Field]() -> Field) -> Field { | ||
f() | ||
} | ||
|
||
// accepts closure that has 1 thing in its env and returns it | ||
fn accepts_closure2(f: fn[Field]() -> Field) -> fn[Field]() -> Field { | ||
f | ||
} | ||
|
||
// accepts closure with different types in the capture group | ||
fn accepts_closure3(f: fn[u32, u64]() -> u64) -> u64 { | ||
f() | ||
} | ||
|
||
fn main() { | ||
assert(ret_normal_lambda1()() == 10); | ||
assert(ret_normal_lambda2()() == 20); | ||
assert(ret_closure1()() == 30); | ||
assert(ret_closure2()() == 40); | ||
assert(ret_closure3()() == 40); | ||
|
||
let x = 50; | ||
assert(accepts_closure1(|| x) == 50); | ||
assert(accepts_closure2(|| x + 10)() == 60); | ||
|
||
let y: u32 = 30; | ||
let z: u64 = 40; | ||
assert(accepts_closure3(|| y as u64 + z) == 70); | ||
} |
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
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
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
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