{"record":{"id":"0634d5a38da22b89","repo":"apple/pkl","slug":"typemismatch-0634d5","errorCode":"typeMismatch","errorMessage":"Expected value of type `{0}`, but got type `{1}`.","messagePattern":"Expected value of type `(.+?)`, but got type `(.+?)`\\.","errorType":"exception","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorPredicateMemberNode.java","lineNumber":116,"sourceCode":"        if (value == null) {\n          var constantValue = member.getConstantValue();\n          if (constantValue != null) {\n            value = constantValue;\n          } else {\n            var callTarget = member.getCallTarget();\n            value = callTarget.call(parent, owner, key);\n          }\n          owner.setCachedValue(key, value);\n        }\n\n        frame.setAuxiliarySlot(customThisSlot, value);\n\n        try {\n          var isApplicable = predicateNode.executeBoolean(frame);\n          if (isApplicable) data.addMember(frame, key, this.member, this);\n        } catch (UnexpectedResultException e) {\n          CompilerDirectives.transferToInterpreter();\n          throw exceptionBuilder()\n              .typeMismatch(e.getResult(), BaseModule.getBooleanClass())\n              .withLocation(predicateNode)\n              .build();\n        }\n      }\n    }\n\n    // restore previous value\n    // handles the (pathetic) case of a predicate containing an object with another predicate\n    frame.setAuxiliarySlot(customThisSlot, previousValue);\n  }\n\n  private void initThisSlot(VirtualFrame frame) {\n    if (customThisSlot == -1) {\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      // deferred until execution time s.t. nodes of inlined type aliases get the right frame slot\n      customThisSlot =\n          frame.getFrameDescriptor().findOrAddAuxiliarySlot(VmUtils.CUSTOM_THIS_FRAME_SLOT_ID);","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/generator/GeneratorPredicateMemberNode.java#L98-L134","documentation":"The predicate of a `when` member must evaluate to a Boolean. When the predicate node's boolean execution fails with an UnexpectedResultException, Pkl reports the actual value's type against the expected `Boolean` type.","triggerScenarios":"`when (expr) { ... }` where `expr` evaluates to a non-Boolean (e.g. a String, Int, or null) inside a Dynamic, Mapping, or Listing generator; raised in addMembers during evalDynamic/evalMapping/evalListing.","commonSituations":"Using a truthy non-Boolean condition (e.g. `when (config.enabled)` where enabled is a String \"yes\"); forgetting `!= null` and passing null into the predicate; returning an Int count where a comparison was intended.","solutions":["Make the predicate explicitly Boolean: use comparisons (`x == y`, `x != null`) rather than raw values.","Convert or validate the value before the predicate (`x.isEmpty()`, `x > 0`, `x != null`).","Check the actual value type reported in the error and fix the underlying property's type or default."],"exampleFix":"// before\nwhen (config.enabled) { ... } // enabled is a String\n// after\nwhen (config.enabled == \"yes\") { ... }","handlingStrategy":"validation","validationCode":"// Ensure predicates are Boolean before use\nwhen (config.enabled is Boolean) {\n  when (config.enabled) { ... }\n}","typeGuard":"function isBoolean(v: Any): Boolean = v is Boolean","tryCatchPattern":null,"preventionTips":["Always write predicates as explicit comparisons (`==`, `!= null`, `> 0`).","Avoid relying on truthiness — Pkl requires strict Booleans.","Declare conditional inputs as `Boolean` in your schema so mistakes surface at load time."],"tags":["pkl","type-mismatch","boolean","when"],"backgroundTag":"type-mismatch","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}