{"record":{"id":"760be0affc2781c2","repo":"apple/pkl","slug":"multipleuniondefaults","errorCode":"multipleUnionDefaults","errorMessage":"A type union cannot have more than one default type.","messagePattern":"A type union cannot have more than one default type\\.","errorType":"exception","errorClass":"GenericParserError","httpStatus":null,"severity":"error","filePath":"pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java","lineNumber":1146,"sourceCode":"    var first = parseTypeAtom(expectation);\n    children.add(first);\n\n    if (lookahead() != Token.UNION) {\n      if (hasDefault) {\n        //noinspection ConstantValue (NullAway needs this assertion, IntelliJ doesn't)\n        assert start != null;\n        throw parserError(ErrorMessages.create(\"notAUnion\"), start.endWith(first.span));\n      }\n      return first;\n    }\n\n    while (lookahead() == Token.UNION) {\n      ff(children);\n      children.add(makeTerminal(next()));\n      ff(children);\n      if (lookahead == Token.STAR) {\n        if (hasDefault) {\n          throw parserError(\"multipleUnionDefaults\");\n        }\n        children.add(makeTerminal(next()));\n        ff(children);\n        hasDefault = true;\n      }\n      var type = parseTypeAtom(expectation);\n      children.add(type);\n    }\n    return new Node(NodeType.UNION_TYPE, children);\n  }\n\n  private Node parseTypeAtom(@Nullable String expectation) {\n    var typ =\n        switch (lookahead) {\n          case UNKNOWN -> make(NodeType.UNKNOWN_TYPE, next().span);\n          case NOTHING -> make(NodeType.NOTHING_TYPE, next().span);\n          case MODULE -> make(NodeType.MODULE_TYPE, next().span);\n          case THIS -> make(NodeType.THIS_TYPE, next().span);","sourceCodeStart":1128,"sourceCodeEnd":1164,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-parser/src/main/java/org/pkl/parser/GenericParserImpl.java#L1128-L1164","documentation":"Within one type union, at most one member may carry the default marker `*`. parseType tracks hasDefault; when a second `*` is seen while parsing union members, it throws multipleUnionDefaults. A union default must be unambiguous to be usable in Pkl's type inference and amending semantics.","triggerScenarios":"Writing a union like `*\"a\"|*\"b\"` where two members both start with `*`.","commonSituations":"Editing a union and accidentally duplicating the `*` prefix when adding a member; bulk-replacing types that each had their own default; macros/templating that prepend `*` to every member.","solutions":["Keep exactly one `*` member; remove `*` from all but the intended default.","Decide which member should be the default and mark only that one.","Search the union declaration for repeated `*` occurrences."],"exampleFix":"// before\nx: *\"a\"|*\"b\"\n\n// after\nx: *\"a\"|\"b\"","handlingStrategy":"validation","validationCode":"// At most one `*` default marker per union\nfunction singleUnionDefault(typeExpr) {\n  return (typeExpr.match(/\\*/g) || []).length <= 1;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide the default member first, then add `|member` entries without `*`.","Avoid templates that stamp `*` onto every union member.","Review unions after bulk type edits for duplicated markers."],"tags":["pkl","parser","type-system","union"],"backgroundTag":"invalid-argument-value","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"}