xai-org/x-algorithm · error · SemanticCheckFailure

Sort is expected to return %s but passed function returns %s

Error message

Sort is expected to return %s but passed function returns %s

What it means

Sort's comparator function must return a Long (numeric ordering key). If the compiled comparator lambda returns any type divergent from Type.LONG, the compiler rejects the Sort expression.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/compiler/Compiler.java:1017

      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(), Type.LONG)) {
        throw new SemanticCheckFailure(
            String.format(
                "Sort is expected to return %s but passed function returns %s",
                Type.LONG,
                funcNode.getReturnType()));
      }

      context.removeScope();

      ASTNode result = Sort.toSort(
          exprText, varA, varB, funcNode, context.currentScopeId(), collectionNode);
      return result;
    } else if (root.getChildCount() == 2) {
      ASTNode collectionNode = createASTNodeTree(context, root.getChild(1));
      Type elementType = Type.OBJECT;
      if (Collection.class.isAssignableFrom(collectionNode.getReturnType().typeBase)) {
        elementType = collectionNode.getReturnType().getTypeParams().get(0);
      }

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Make the comparator return a Long (wrap comparisons in If(...) producing 0L/1L, or cast with ToLong)
  2. If sorting by an object key, compute a numeric sort key inside the lambda

Example fix

// before
Sort(users, (a, b) -> a.name < b.name)
// after
Sort(users, (a, b) -> If(a.name < b.name, -1L, 1L))
Defensive patterns

Strategy: type-guard

Type guard

// validate comparator returns Long before compiling Sort
if (!LONG.equals(comparatorReturnType)) throw new IllegalArgumentException("Sort comparator must return Long");

Try / catch

catch (SemanticCheckFailure e) { if (e.getMessage().contains("Sort is expected")) surfaceTypeHint(e); else throw e; }

Prevention

When it happens

Trigger: `Sort(coll, fn)` where fn returns Double, String, Boolean, or a struct instead of Long.

Common situations: Writing a comparator that returns the result of a string comparison helper, a boolean predicate, or forgetting to coerce a numeric computation to a long.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/358f0caee4b766e7. Report an issue: GitHub.