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

  1. Ensure the annotated value is actually a function literal with the exact parameter count of the function type (e.g. `(a, b) -> ...` for Function2).
  2. If any function is acceptable, widen the annotation to `Function` instead of `FunctionN`.
  3. Check for typos or trailing/missing parameters in the function literal.
  4. 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

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


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/09a90d024acbe68d. Report an issue: GitHub.