{"record":{"id":"42262b5e8b866e22","repo":"xai-org/x-algorithm","slug":"foldl1-is-expected-to-return-s-but-passed-functio","errorCode":null,"errorMessage":"Foldl1 is expected to return %s but passed function returns %s","messagePattern":"Foldl1 is expected to return (.+?) but passed function returns (.+?)","errorType":"validation","errorClass":"SemanticCheckFailure","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/compiler/Compiler.java","lineNumber":968,"sourceCode":"    Type variableType = Type.OBJECT;\n    if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {\n      variableType = collectionNode.getReturnType().getTypeParams().get(0);\n    }\n\n    CompilerScope scope = context.addScope();\n    long scopeId = context.currentScopeId();\n\n    Parameter parameterA = Parameter.scope(\n        varA, variableType, scopeId, collectionNode);\n    scope.defineVariable(parameterA);\n\n    Parameter parameterB = Parameter.scope(\n        varB, variableType, scopeId, collectionNode);\n    scope.defineVariable(parameterB);\n\n    ASTNode funcNode = createASTNodeTree(context, root.getChild(3));\n    if (Type.isDivergentTo(funcNode.getReturnType(), variableType)) {\n      throw new SemanticCheckFailure(\n          String.format(\n              \"Foldl1 is expected to return %s but passed function returns %s\",\n              variableType,\n              funcNode.getReturnType()));\n    }\n\n    context.removeScope();\n\n    ASTNode result = Fold.createFoldl1(\n        exprText, varA, varB, funcNode, context.currentScopeId(), collectionNode);\n    return result;\n  }\n\n  private ASTNode toInputFeature(CompilerContext context, Tree root)\n      throws SemanticCheckFailure {\n    String featureName = toStringConstant(root.getChild(1));\n    Type type = getInputFeatureType(context.getInputFeatureNameSpace(), featureName);\n","sourceCodeStart":950,"sourceCodeEnd":986,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/compiler/Compiler.java#L950-L986","documentation":"Foldl1 folds a collection without an explicit seed, so the accumulator type is the collection's element type; the passed function must return that same type. This error fires when the function's return type diverges from the element (variable) type.","triggerScenarios":"`Foldl1(coll, fn)` where fn returns a different type than coll's elements, e.g. elements are Long but fn returns Double or a struct.","commonSituations":"Reusing a Foldl lambda (with seed) in Foldl1 where types differ, or aggregating elements into a summary object without realizing Foldl1 fixes the accumulator type.","solutions":["Change the function to return the collection's element type","If you need a different accumulator type, use Foldl with an explicit seed of the desired type"],"exampleFix":"// before\nFoldl1(longs, (a, b) -> ToDouble(a) + ToDouble(b))\n// after\nFoldl(longs, 0.0, (a, b) -> a + ToDouble(b))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// ensure Foldl1 fn returns the collection element type before compiling\nif (!fnReturnType.equals(elementType)) throw new IllegalArgumentException(\"Foldl1 type mismatch\");","tryCatchPattern":"catch (SemanticCheckFailure e) { if (e.getMessage().contains(\"Foldl1\")) surfaceTypeHint(e); else throw e; }","preventionTips":["Prefer explicit-seed Foldl when accumulator type differs from element type"],"tags":["botmaker","foldl1","type-mismatch"],"backgroundTag":"fold-type-mismatch","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}