{"record":{"id":"60c2273a93160486","repo":"apple/pkl","slug":"wrongtypeargumentcount","errorCode":"wrongTypeArgumentCount","errorMessage":"wrongTypeArgumentCount","messagePattern":"wrongTypeArgumentCount","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/reflect/DeclaredTypeNodes.java","lineNumber":58,"sourceCode":"\n  public abstract static class withTypeArguments extends ExternalMethod1Node {\n    @Specialization\n    @TruffleBoundary\n    protected VmTyped eval(VmTyped self, VmList typeArguments) {\n      var referent = (VmTyped) VmUtils.readMember(self, REFERENT);\n      checkTypeArgumentCount(referent, typeArguments.getLength(), this);\n      return MirrorFactories.declaredTypeFactory.create(Pair.of(referent, typeArguments));\n    }\n  }\n\n  private static void checkTypeArgumentCount(VmTyped referent, int actualCount, PklNode node) {\n    var extraStorage = referent.getExtraStorage();\n    var typeParameterCount =\n        extraStorage instanceof VmClass vmClass\n            ? vmClass.getTypeParameterCount()\n            : ((VmTypeAlias) extraStorage).getTypeParameterCount();\n    if (typeParameterCount != actualCount) {\n      throw new VmExceptionBuilder()\n          .evalError(\"wrongTypeArgumentCount\", typeParameterCount, actualCount)\n          .withLocation(node)\n          .build();\n    }\n  }\n}\n","sourceCodeStart":40,"sourceCodeEnd":65,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/reflect/DeclaredTypeNodes.java#L40-L65","documentation":"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.","triggerScenarios":"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).","commonSituations":"Reflection or tooling code that constructs parameterized types programmatically (PklSwift/Pkl API bindings) after a library upgrade changed type-parameter counts.","solutions":["Supply the exact number of type arguments the referent declares (check `getTypeParameterCount`)","For Map use two arguments (key, value); for Listing/Set/Dynamic use one; for non-generic classes use zero","Regenerate client bindings after upgrading the pkl dependency","Read the counts in the message: expected vs. actual"],"exampleFix":"// before\nnew DeclaredType(VmMap, [stringType])            // Map needs 2\n// after\nnew DeclaredType(VmMap, [stringType, intType])","handlingStrategy":"validation","validationCode":"// Java-side check before constructing the DeclaredType\nint expected = referent instanceof VmClass c ? c.getTypeParameterCount() : ((VmTypeAlias) t).getTypeParameterCount();\nif (expected != typeArgs.size()) throw new IllegalArgumentException(\"need \" + expected + \" type args\");","typeGuard":"boolean hasCorrectArity(VmTyped referent, List<?> args) { return typeParamCount(referent) == args.size(); }","tryCatchPattern":"try { buildDeclaredType(referent, args); } catch (PklException e) { if (e.getMessage().contains(\"wrongTypeArgumentCount\")) { fixArityAndRetry(); } else throw e; }","preventionTips":["Check getTypeParameterCount() before passing type arguments","Regenerate bindings when upgrading the pkl runtime","Write unit tests constructing each generic type you use"],"tags":["reflection","type-arguments","generics","pkl"],"backgroundTag":"wrong-type-argument-count","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}