{"record":{"id":"b209455b2e93adf9","repo":"apple/pkl","slug":"interpolationinconstant","errorCode":"interpolationInConstant","errorMessage":"String constant cannot have interpolated values.","messagePattern":"String constant cannot have interpolated values\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":1396,"sourceCode":"  private Node parseStringConstant() {\n    var children = new ArrayList<Node>();\n    var startTk = expect(Token.STRING_START, \"unexpectedToken\", \"\\\"\");\n    children.add(makeTerminal(startTk));\n    while (lookahead != Token.STRING_END) {\n      switch (lookahead) {\n        case STRING_PART,\n            STRING_ESCAPE_NEWLINE,\n            STRING_ESCAPE_TAB,\n            STRING_ESCAPE_QUOTE,\n            STRING_ESCAPE_BACKSLASH,\n            STRING_ESCAPE_RETURN,\n            STRING_ESCAPE_UNICODE ->\n            children.add(makeTerminal(next()));\n        case EOF -> {\n          var delimiter = new StringBuilder(startTk.text(lexer)).reverse().toString();\n          throw parserError(\"missingDelimiter\", delimiter);\n        }\n        case INTERPOLATION_START -> throw parserError(\"interpolationInConstant\");\n        // the lexer makes sure we only get the above tokens inside a string\n        default -> throw new RuntimeException(\"Unreacheable code\");\n      }\n    }\n    children.add(makeTerminal(next())); // string end\n    return new Node(NodeType.STRING_CHARS, children);\n  }\n\n  private FullToken expect(Token type, String errorKey, Object... messageArgs) {\n    if (lookahead != type) {\n      var span = spanLookahead;\n      if (lookahead == Token.EOF || _lookahead.newLinesBetween > 0) {\n        // don't point at the EOF or the next line, but at the end of the last token\n        span = prev().span.stopSpan();\n      }\n      var args = messageArgs;\n      if (errorKey.startsWith(\"unexpectedToken\")) {\n        args = new Object[messageArgs.length + 1];","sourceCodeStart":1378,"sourceCodeEnd":1414,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L1378-L1414","documentation":"Pkl string constants (plain quoted strings) do not support `\\(...)` interpolation; only standard strings may interpolate. When the parser encounters INTERPOLATION_START inside a string constant it throws this dedicated error. This distinguishes constant strings (which are interned/exact) from interpolating strings.","triggerScenarios":"Using `\\(expr)` inside a string constant produced in parseStringConstant(), e.g. `const = \"value \\(x)\"` in a context where the string is lexed as a constant (such as certain module-level or annotation contexts).","commonSituations":"Porting Kotlin/Swift-style interpolation habits into Pkl constant strings; moving a string between contexts where the lexer classifies it differently; assuming all Pkl strings interpolate.","solutions":["Use Pkl's standard string interpolation form `\"\\(expr)\"` in a regular (non-constant) string context.","Concatenate values explicitly if the context requires a constant: `\"a \" + x`.","Remove the interpolation and hardcode the value if the string must remain constant."],"exampleFix":"// before (in a string-constant position)\ngreeting = \"Hello \\(name)\"\n\n// after\ngreeting = \"Hello \" + name","handlingStrategy":"validation","validationCode":"function assertNoInterpolationInConstant(str, isConstantContext) {\n  if (isConstantContext && /\\\\\\(/.test(str)) {\n    throw new Error(`String constant cannot interpolate: ${str}`);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Know which Pkl positions accept constants (annotations, some defaults) and keep them static.","Use concatenation (`\"a \" + expr`) instead of `\\(...)` when in doubt.","Lint generated Pkl for `\\(` inside constant contexts."],"tags":["parser","string","interpolation","syntax-error"],"backgroundTag":"unsupported-operation","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"}