{"record":{"id":"dfdaf7f7a8719fad","repo":"BoundaryML/baml","slug":"cannot-assign-function-to-variable","errorCode":null,"errorMessage":"cannot assign function to variable `{}`","messagePattern":"cannot assign function to variable `(.+?)`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":583,"sourceCode":"                                    if let Some(var_ref) = lookup_variable(scopes, name) {\n                                        register_watch_variable(\n                                            scopes,\n                                            name,\n                                            var_ref,\n                                            watch_spec.clone(),\n                                        );\n                                    }\n                                }\n                            }\n                            EvalValue::Reference(cell) => {\n                                declare_with_cell(scopes, name, cell.clone());\n                                // Register watch tracking if @watch is present\n                                if let Some(emit_spec) = watch {\n                                    register_watch_variable(scopes, name, cell, emit_spec.clone());\n                                }\n                            }\n                            EvalValue::Function(_, _, _) => {\n                                bail!(\"cannot assign function to variable `{}`\", name);\n                            }\n                        }\n                        // Check for changes in all watch variables after the let statement\n                        check_watch_changes(\n                            scopes,\n                            watch_handler,\n                            function_name,\n                            thir,\n                            run_llm_function,\n                        )\n                        .await;\n                    }\n                    Statement::Declare { name, span } => {\n                        declare(scopes, name, BamlValueWithMeta::Null((span.clone(), None)));\n                    }\n                    Statement::Assign { left, value } => {\n                        let assigned_value = expect_value(\n                            evaluate_expr(","sourceCodeStart":565,"sourceCodeEnd":601,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L565-L601","documentation":"In the BAML interpreter, a let statement binds a value to a new variable, but first-class function values cannot be stored in variables. When the evaluated right-hand side is EvalValue::Function, handle_statement bails out instead of creating the binding. (Assignment of function values is supported by codegen paths only in limited ways; the interpreter rejects it here.)","triggerScenarios":"Executing let name = <expression evaluating to a function> in interpreted code — e.g. let f = my_function or let f = if c { fn_a } else { fn_b }.","commonSituations":"Trying to alias functions for dynamic dispatch; translating higher-order patterns from other languages into BAML; refactoring shared call sites into a variable.","solutions":["Call the function directly by name at each use site instead of aliasing it","Replace the alias with an explicit conditional that calls the appropriate named function inline","Restructure so the function is invoked where defined rather than passed/stored"],"exampleFix":"// before\nlet handler = on_event\nhandler(x)\n// after\non_event(x)","handlingStrategy":"validation","validationCode":"fn validate_let_value(v: &EvalValue, name: &str) -> Result<(), String> {\n    match v { EvalValue::Function(_, _, _) => Err(format!(\"cannot assign function to '{}'\", name)), _ => Ok(()) }\n}","typeGuard":"fn is_assignable(v: &EvalValue) -> bool { !matches!(v, EvalValue::Function(_, _, _)) }","tryCatchPattern":null,"preventionTips":["Do not alias functions to variables in interpreted BAML","Call functions directly by name at use sites","Replace dynamic-dispatch patterns with explicit if/else over named calls"],"tags":["interpreter","functions","assignment","unsupported-feature"],"backgroundTag":"unsupported-operation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}