{"record":{"id":"09a90d024acbe68d","repo":"apple/pkl","slug":"type-mismatch-value-is-not-a-function-n-with-exp","errorCode":null,"errorMessage":"type mismatch: value is not a Function<N> with expected parameter count","messagePattern":"type mismatch: value is not a Function<N> with expected parameter count","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":2031,"sourceCode":"          Arrays.stream(parameterTypeNodes).map(TypeNode::export).collect(Collectors.toList());\n      return new PType.Function(parameterTypes, returnTypeNode.doExport());\n    }\n\n    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n\n    @SuppressWarnings(\"unused\")\n    @Specialization(guards = \"value.getVmClass() == getFunctionNClass()\")\n    protected Object eval(VmFunction value) {\n      /* do nothing */\n      return value;\n    }\n\n    @Fallback\n    protected Object fallback(Object value) {\n      throw typeMismatch(value, getFunctionNClass());\n    }\n\n    // not a field to avoid a circular evaluation error\n    protected VmClass getFunctionNClass() {\n      return BaseModule.getFunctionNClass(parameterTypeNodes.length);\n    }\n\n    @Override\n    protected boolean isParametric() {\n      return true;\n    }\n  }\n\n  // A type such as `Function<Duration>` (but not `FunctionN<...>`).\n  public abstract static class FunctionClassTypeNode extends ObjectSlotTypeNode {\n    private final TypeNode typeArgumentNode;\n\n    protected FunctionClassTypeNode(SourceSection sourceSection, TypeNode typeArgumentNode) {","sourceCodeStart":2013,"sourceCodeEnd":2049,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L2013-L2049","documentation":"Thrown by the @Fallback path of a Function<N> check node in Pkl's type checker when a value fails the `is FunctionN` (or typed function) test. Truffle ASTs specialize on the expected value shape; this fallback is the slow path that reports the type mismatch. N is the parameter count of the function type in the annotation.","triggerScenarios":"A type annotation like `x: Function2(Int, Int) -> String` (or `Function1<...>` etc.) is checked against a value that is not a pkl function at all (e.g. an Int, String, Listing), or is a function with a different parameter count.","commonSituations":"Config mistakes where a field expects a function but the user assigned a plain value; passing a lambda-like expression with the wrong arity; confusing `Function` (any) with `FunctionN` (specific arity) in a type annotation.","solutions":["Ensure the annotated value is actually a function literal with the exact parameter count of the function type (e.g. `(a, b) -> ...` for Function2).","If any function is acceptable, widen the annotation to `Function` instead of `FunctionN`.","Check for typos or trailing/missing parameters in the function literal.","If the value comes from an import or generated code, inspect its declared type to match the expected arity."],"exampleFix":"// before\nx: Function2(Int, Int) -> String = (a) -> a\n\n// after\nx: Function2(Int, Int) -> String = (a, b) -> a + b","handlingStrategy":"type-guard","validationCode":"// in Pkl, before binding:\nif (value is Function) {\n  // value is a function; still verify arity matches the FunctionN annotation\n}","typeGuard":"function isFunctionN(v: Any, arity: Int): Boolean = v is Function && /* arity inferred from type */ true","tryCatchPattern":null,"preventionTips":["Count parameters in the function literal against the FunctionN arity.","Prefer plain Function annotations when any arity is acceptable.","Run `pkl eval` on configs before deploy to surface type checks early."],"tags":["pkl","type-mismatch","function-type","truffle"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}