{"record":{"id":"3883f2e79292536b","repo":"apple/pkl","slug":"operatornotdefined1","errorCode":"operatorNotDefined1","errorMessage":"operatorNotDefined1","messagePattern":"operatorNotDefined1","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/unary/UnaryExpressionNode.java","lineNumber":34,"sourceCode":"package org.pkl.core.ast.expression.unary;\n\nimport com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;\nimport com.oracle.truffle.api.dsl.Fallback;\nimport com.oracle.truffle.api.dsl.NodeChild;\nimport com.oracle.truffle.api.source.SourceSection;\nimport org.pkl.core.ast.ExpressionNode;\nimport org.pkl.core.runtime.VmUtils;\n\n@NodeChild(value = \"operandNode\", type = ExpressionNode.class)\npublic abstract class UnaryExpressionNode extends ExpressionNode {\n  protected UnaryExpressionNode(SourceSection sourceSection) {\n    super(sourceSection);\n  }\n\n  @Fallback\n  @TruffleBoundary\n  protected Object fallback(Object operand) {\n    throw exceptionBuilder()\n        .evalError(\"operatorNotDefined1\", getShortName(), VmUtils.getClass(operand))\n        .withProgramValue(\"Operand\", operand)\n        .build();\n  }\n}\n","sourceCodeStart":16,"sourceCodeEnd":40,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/unary/UnaryExpressionNode.java#L16-L40","documentation":"Pkl raises `operatorNotDefined1` when a unary operator is applied to an operand whose type does not support it. The UnaryExpressionNode family (e.g. UnaryMinusNode, LogicalNotNode) uses Truffle specializations per type; when no specialization matches, the @Fallback at UnaryExpressionNode.java:34 throws an eval error naming the operator and the operand's class. This is Pkl's way of saying the operand type has no defined semantics for that operator.","triggerScenarios":"Evaluating a unary operator in Pkl code where the operand type is not covered by any specialization, e.g. `-\"abc\"` (unary minus on String), `!42` (logical not on Int), or applying a unary operator to a Listing/Mapping/typed object or null where no specialization exists. `getShortName()` supplies the operator symbol and `VmUtils.getClass(operand)` supplies the offending operand type in the message.","commonSituations":"A property intended to be a number is actually a String (so `-value` fails); a boolean property reads null or a non-Boolean from an environment override so `!flag` fails; refactored code changed a property's type (Int -> String) leaving a unary expression behind; applying `!` to the result of a comparison that was removed during editing.","solutions":["Read the error message: it names the operator and the operand's class; locate the operand expression at the reported source location and confirm its actual type.","Convert or fix the operand so it has the type the operator expects (e.g. use `value.toBoolean()` style conversion or fix the value so `-` applies to Int/Float and `!` applies to Boolean).","If the operand can legitimately be absent/null, guard the expression, e.g. use the null-propagating form or a conditional instead of the raw operator.","If the type is dynamic (external input), add an `is`-based type check or a `when`/amend block so the operator is only applied to matching values."],"exampleFix":"// before (pkl)\nflag: !input.enabled   // input.enabled is a String \"yes\" -> operatorNotDefined1\n\n// after\nflag: !input.enabled.toBoolean()","handlingStrategy":"type-guard","validationCode":"// pkl\nif (value is Int || value is Float) { /* safe for unary minus */ }\nif (value is Boolean) { /* safe for ! */ }","typeGuard":"function isNumeric(x) = x is Int || x is Float\nfunction isBool(x) = x is Boolean","tryCatchPattern":null,"preventionTips":["Type-annotate module/class properties so wrong-type values fail at validation, not at the operator.","Convert external input (env vars, CLI) to the intended type before applying operators.","Avoid mixing dynamic external values directly into arithmetic/logic expressions."],"tags":["pkl","unary-operator","type-error","truffle-fallback"],"backgroundTag":"unsupported-operation","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"}