xai-org/x-algorithm · error · SemanticCheckFailure
Foldl1 is expected to return %s but passed function returns
Error message
Foldl1 is expected to return %s but passed function returns %s
What it means
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.
Source
Thrown at botmaker/src/java/com/twitter/botmaker/compiler/Compiler.java:968
Type variableType = Type.OBJECT;
if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {
variableType = collectionNode.getReturnType().getTypeParams().get(0);
}
CompilerScope scope = context.addScope();
long scopeId = context.currentScopeId();
Parameter parameterA = Parameter.scope(
varA, variableType, scopeId, collectionNode);
scope.defineVariable(parameterA);
Parameter parameterB = Parameter.scope(
varB, variableType, scopeId, collectionNode);
scope.defineVariable(parameterB);
ASTNode funcNode = createASTNodeTree(context, root.getChild(3));
if (Type.isDivergentTo(funcNode.getReturnType(), variableType)) {
throw new SemanticCheckFailure(
String.format(
"Foldl1 is expected to return %s but passed function returns %s",
variableType,
funcNode.getReturnType()));
}
context.removeScope();
ASTNode result = Fold.createFoldl1(
exprText, varA, varB, funcNode, context.currentScopeId(), collectionNode);
return result;
}
private ASTNode toInputFeature(CompilerContext context, Tree root)
throws SemanticCheckFailure {
String featureName = toStringConstant(root.getChild(1));
Type type = getInputFeatureType(context.getInputFeatureNameSpace(), featureName);
View on GitHub (pinned to 24c60942c5)
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
Example fix
// before Foldl1(longs, (a, b) -> ToDouble(a) + ToDouble(b)) // after Foldl(longs, 0.0, (a, b) -> a + ToDouble(b))
Defensive patterns
Strategy: type-guard
Type guard
// ensure Foldl1 fn returns the collection element type before compiling
if (!fnReturnType.equals(elementType)) throw new IllegalArgumentException("Foldl1 type mismatch"); Try / catch
catch (SemanticCheckFailure e) { if (e.getMessage().contains("Foldl1")) surfaceTypeHint(e); else throw e; } Prevention
- Prefer explicit-seed Foldl when accumulator type differs from element type
When it happens
Trigger: `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.
Common situations: 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.
Related errors
- function passed to Foldl() returns %s but seed value is a %s
- Sort is expected to return %s but passed function returns %s
- Parameter #%d of %s should be %s, received %s instead
- Contains expects input of String, Map, Set or Collection typ
- Copy function only supports Map or StructTuple.
AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28).
Data as JSON: /api/errors/42262b5e8b866e22.
Report an issue: GitHub.