Updated closure syntax

This commit is contained in:
2026-06-23 17:41:49 +02:00
parent e4b7b4abf9
commit 2f12f5ac53
3 changed files with 8 additions and 8 deletions
+6 -6
View File
@@ -30,7 +30,6 @@ keyword = {
| "false" | "false"
| "dyn" | "dyn"
| "loop" | "loop"
| "fn"
| "unsafe" | "unsafe"
| "override" | "override"
} }
@@ -200,8 +199,8 @@ attributes = {
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
var_decl = { var_decl = {
("let" ~ pattern) | ("let" ~ pattern)
type_expr ~ (!tuple_pattern ~ pattern) | type_expr ~ (!tuple_pattern ~ pattern)
} }
field = { field = {
@@ -343,7 +342,8 @@ function_decl = {
// ====================================================== // ======================================================
top_level = { top_level = {
mod_attribute | (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) mod_attribute
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
} }
// ====================================================== // ======================================================
@@ -466,8 +466,8 @@ match_stmt = {
// ------------------------------------------------------ // ------------------------------------------------------
closure = { closure = {
"fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ block ("(" ~ param_list ~ ")" ~ "=>" ~ expr)
| "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ "->" ~ expr | (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr)
} }
// ------------------------------------------------------ // ------------------------------------------------------
+1 -1
View File
@@ -77,10 +77,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
Rule::closure => { Rule::closure => {
ast_expr!(Expression::Closure { ast_expr!(Expression::Closure {
params: collect_recovered(inner.next().unwrap().into_inner()),
return_type: consume_rule(&mut inner, Rule::type_expr) return_type: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose(), .transpose(),
params: collect_recovered(inner.next().unwrap().into_inner()),
body: Expression::try_from(inner.next().unwrap()).map(Box::new), body: Expression::try_from(inner.next().unwrap()).map(Box::new),
}) })
} }
+1 -1
View File
@@ -88,7 +88,7 @@ pub void test_as_cast() {
} }
pub void test_closure() { pub void test_closure() {
let add = fn(a, b) -> a + b; let add = (a, b) => a + b;
assert_eq!(add(2, 3), 5); assert_eq!(add(2, 3), 5);
} }