{"record":{"id":"7dac403769aa7c80","repo":"apple/pkl","slug":"typemismatch-7dac40","errorCode":null,"errorMessage":"typeMismatch","messagePattern":"typeMismatch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/lambda/ApplyVmFunction2Node.java","lineNumber":35,"sourceCode":"\nimport com.oracle.truffle.api.CompilerDirectives;\nimport com.oracle.truffle.api.RootCallTarget;\nimport com.oracle.truffle.api.dsl.Cached;\nimport com.oracle.truffle.api.dsl.Specialization;\nimport com.oracle.truffle.api.nodes.DirectCallNode;\nimport com.oracle.truffle.api.nodes.IndirectCallNode;\nimport org.pkl.core.ast.PklNode;\nimport org.pkl.core.runtime.*;\n\npublic abstract class ApplyVmFunction2Node extends PklNode {\n  public abstract Object execute(VmFunction function, Object arg1, Object arg2);\n\n  public final boolean executeBoolean(VmFunction function, Object arg1, Object arg2) {\n    var result = execute(function, arg1, arg2);\n    if (result instanceof Boolean b) return b;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getBooleanClass()).build();\n  }\n\n  public final VmCollection executeCollection(VmFunction function, Object arg1, Object arg2) {\n    var result = execute(function, arg1, arg2);\n    if (result instanceof VmCollection collection) return collection;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getCollectionClass()).build();\n  }\n\n  public final VmMap executeMap(VmFunction function, Object arg1, Object arg2) {\n    var result = execute(function, arg1, arg2);\n    if (result instanceof VmMap map) return map;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder().typeMismatch(result, BaseModule.getMapClass()).build();\n  }\n","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/lambda/ApplyVmFunction2Node.java#L17-L53","documentation":"Pkl's internal `ApplyVmFunction2Node.executeBoolean` calls a two-argument Pkl function and requires the result to be a Boolean. If the result is not a Boolean, a `typeMismatch` error is thrown against `base::Boolean`. This is used by call sites (e.g. binary-operator-style function application) that assume a Boolean return.","triggerScenarios":"A two-argument function (typically a comparator or binary predicate, e.g. passed to sort/compare operators) is invoked via executeBoolean and returns a non-Boolean value such as Int, String, or null.","commonSituations":"A comparator lambda returns the compared values or a difference instead of a Boolean test; a predicate omits a branch so it evaluates to null; using a key-extraction function where a comparator was expected.","solutions":["Make the two-argument function a proper predicate: its body must evaluate to a Boolean comparison (e.g. `a <= b`).","Cover all branches so the function never evaluates to null.","If you meant to provide a key/transform function, pass it to the parameter that expects that role rather than the comparator role.","Assert the final expression with a comparison to catch accidental non-Boolean results."],"exampleFix":"// before (pkl)\nsorted(items, (a, b) -> a.length - b.length)   // Int result -> typeMismatch (expected Boolean)\n\n// after\nsorted(items, (a, b) -> a.length <= b.length)","handlingStrategy":"type-guard","validationCode":"// pkl\nassert(comparatorResult is Boolean, \"comparator must return a Boolean\")","typeGuard":"function isBool2(f: (Any, Any) -> Any, a: Any, b: Any): Boolean = f(a, b) is Boolean","tryCatchPattern":null,"preventionTips":["Express comparators as Boolean tests (`a <= b`), never as differences or returned values.","Ensure both branches of a conditional comparator yield Booleans."],"tags":["pkl","type-mismatch","function-result","boolean"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}