apple/pkl · error · VmTypeMismatchException
type mismatch: value is not a Function<N> with expected para
Error message
type mismatch: value is not a Function<N> with expected parameter count
What it means
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.
Source
Thrown at pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java:2031
Arrays.stream(parameterTypeNodes).map(TypeNode::export).collect(Collectors.toList());
return new PType.Function(parameterTypes, returnTypeNode.doExport());
}
@Override
protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
return consumer.accept(this);
}
@SuppressWarnings("unused")
@Specialization(guards = "value.getVmClass() == getFunctionNClass()")
protected Object eval(VmFunction value) {
/* do nothing */
return value;
}
@Fallback
protected Object fallback(Object value) {
throw typeMismatch(value, getFunctionNClass());
}
// not a field to avoid a circular evaluation error
protected VmClass getFunctionNClass() {
return BaseModule.getFunctionNClass(parameterTypeNodes.length);
}
@Override
protected boolean isParametric() {
return true;
}
}
// A type such as `Function<Duration>` (but not `FunctionN<...>`).
public abstract static class FunctionClassTypeNode extends ObjectSlotTypeNode {
private final TypeNode typeArgumentNode;
protected FunctionClassTypeNode(SourceSection sourceSection, TypeNode typeArgumentNode) {View on GitHub (pinned to f3efcbfc9b)
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.
Example fix
// before x: Function2(Int, Int) -> String = (a) -> a // after x: Function2(Int, Int) -> String = (a, b) -> a + b
Defensive patterns
Strategy: type-guard
Validate before calling
// in Pkl, before binding:
if (value is Function) {
// value is a function; still verify arity matches the FunctionN annotation
} Type guard
function isFunctionN(v: Any, arity: Int): Boolean = v is Function && /* arity inferred from type */ true
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- type mismatch: value is not of type Function
- Error converting property `%s` in Pkl object of type `%s` to
- The top-level value of a YAML stream must have type `Collect
- type constraint mismatch
- type mismatch
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/09a90d024acbe68d.
Report an issue: GitHub.