apple/pkl · error · VmException

wrongTypeArgumentCount

wrongTypeArgumentCount

Error message

wrongTypeArgumentCount

What it means

This check runs when constructing a declared type reference (e.g. `Foo<Int>` via the reflect/type-argument machinery). It compares the referent's declared type-parameter count with the number of supplied type arguments and throws when they differ.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/stdlib/reflect/DeclaredTypeNodes.java:58

  public abstract static class withTypeArguments extends ExternalMethod1Node {
    @Specialization
    @TruffleBoundary
    protected VmTyped eval(VmTyped self, VmList typeArguments) {
      var referent = (VmTyped) VmUtils.readMember(self, REFERENT);
      checkTypeArgumentCount(referent, typeArguments.getLength(), this);
      return MirrorFactories.declaredTypeFactory.create(Pair.of(referent, typeArguments));
    }
  }

  private static void checkTypeArgumentCount(VmTyped referent, int actualCount, PklNode node) {
    var extraStorage = referent.getExtraStorage();
    var typeParameterCount =
        extraStorage instanceof VmClass vmClass
            ? vmClass.getTypeParameterCount()
            : ((VmTypeAlias) extraStorage).getTypeParameterCount();
    if (typeParameterCount != actualCount) {
      throw new VmExceptionBuilder()
          .evalError("wrongTypeArgumentCount", typeParameterCount, actualCount)
          .withLocation(node)
          .build();
    }
  }
}

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Supply the exact number of type arguments the referent declares (check `getTypeParameterCount`)
  2. For Map use two arguments (key, value); for Listing/Set/Dynamic use one; for non-generic classes use zero
  3. Regenerate client bindings after upgrading the pkl dependency
  4. Read the counts in the message: expected vs. actual

Example fix

// before
new DeclaredType(VmMap, [stringType])            // Map needs 2
// after
new DeclaredType(VmMap, [stringType, intType])
Defensive patterns

Strategy: validation

Validate before calling

// Java-side check before constructing the DeclaredType
int expected = referent instanceof VmClass c ? c.getTypeParameterCount() : ((VmTypeAlias) t).getTypeParameterCount();
if (expected != typeArgs.size()) throw new IllegalArgumentException("need " + expected + " type args");

Type guard

boolean hasCorrectArity(VmTyped referent, List<?> args) { return typeParamCount(referent) == args.size(); }

Try / catch

try { buildDeclaredType(referent, args); } catch (PklException e) { if (e.getMessage().contains("wrongTypeArgumentCount")) { fixArityAndRetry(); } else throw e; }

Prevention

When it happens

Trigger: Building a DeclaredType with the wrong number of type arguments, e.g. `DeclaredType(Listing, [String])` (Listing takes 1) or `DeclaredType(Map, [String])` (Map takes 2).

Common situations: Reflection or tooling code that constructs parameterized types programmatically (PklSwift/Pkl API bindings) after a library upgrade changed type-parameter counts.

Related errors


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