{"record":{"id":"54a852bf452d8a8d","repo":"BoundaryML/baml","slug":"exprs-that-evaluate-to-lambda","errorCode":null,"errorMessage":"exprs that evaluate to lambda","messagePattern":"exprs that evaluate to lambda","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/ast/src/parser/parse_expr.rs","lineNumber":902,"sourceCode":"        | Expression::Paren(..) => true,\n\n        // If the trailing expression happens to be a block, check if the\n        // block itself has a trailing expression that produces a value.\n        Expression::ExprBlock(block, _) => block.expr.is_some(),\n\n        // If trailing expression is an if statement, check if the statment\n        // itself has a trailing expression.\n        Expression::If(_, if_branch, else_branch, _) => match if_branch.as_ref() {\n            Expression::ExprBlock(block, _) => block.expr.is_some(),\n            _ => match else_branch.as_ref().map(Box::as_ref) {\n                Some(Expression::ExprBlock(block, _)) => block.expr.is_some(),\n                // This should not happen since branches are always blocks.\n                _ => true,\n            },\n        },\n\n        // TODO: Is this possible?\n        Expression::Lambda(..) => todo!(\"exprs that evaluate to lambda\"),\n    });\n\n    // If the block actually returns a value, keep it as trailing expression.\n    // Otherwise, promote the expression to a statement.\n    let trailing_expr = if is_return_value {\n        expr.map(Box::new)\n    } else {\n        if let Some(expr) = expr {\n            stmts.push(Stmt::Expression(ExprStmt {\n                expr: expr.clone(),\n                annotations: vec![],\n                span: expr.span().clone(),\n            }));\n        }\n\n        None\n    };\n","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/ast/src/parser/parse_expr.rs#L884-L920","documentation":"This is a `todo!()` panic in `parse_expr_block` in the BAML AST parser. When the parser needs to decide whether the expression inside a block is a return value, it encounters an `Expression::Lambda` and explicitly aborts, because lambdas that appear as expressions evaluating in statement position are not yet supported. The preceding `// TODO: Is this possible?` comment confirms this is an intentionally unimplemented path, not a data corruption bug.","triggerScenarios":"Parsing a BAML block (function body, if/while/for body, or statement block via `parse_expr_block`) whose trailing/statement expression is a lambda, e.g. `() => {...}` appearing where a value-returning expression is evaluated.","commonSituations":"Writing a BAML function whose last statement returns or evaluates a lambda; passing an anonymous function as the result of a block in a prompt-time expression; upgrading BAML and using lambda expressions in places the runtime does not yet handle.","solutions":["Rewrite the BAML code so the block's trailing expression is not a lambda — assign the lambda to a variable or pass it directly as a function argument instead","Return a concrete value (string, int, object) from the block rather than a function value","Check the BAML changelog/releases for lambda-expression support and upgrade if implemented","If you are a contributor: replace the `todo!()` with a proper rule for `Expression::Lambda` (decide is_return_value and emit diagnostics)"],"exampleFix":"// before (panics)\nfunction F(x int) int {\n  () => x + 1\n}\n// after\nfunction F(x int) int {\n  let f = (y int) => y + 1;\n  f(x)\n}","handlingStrategy":"validation","validationCode":"// Reject lambdas in trailing/statement position before submitting to the parser:\nfunction assertNoLambdaTail(blockSource) {\n  if (/=>\\s*(\\{|[^\\n]*$)/m.test(lastStatementOf(blockSource))) {\n    throw new Error('Lambda as block trailing expression is unsupported and panics the BAML parser');\n  }\n}","typeGuard":"const isLambdaExpr = (expr) => typeof expr === 'string' && /=>/.test(expr);","tryCatchPattern":null,"preventionTips":["Do not return or evaluate lambdas as the last expression of a BAML block; bind them to a variable or pass as arguments instead","Track BAML releases for lambda-expression support before using function values as block results","Wrap generated BAML in validation that flags `=>` in trailing position","When contributing, replace todo!() with diagnostics-based errors"],"tags":["parser","panic","unimplemented","lambda"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}