apple/pkl · error

cannotApplyConstModifier

cannotApplyConstModifier

Error message

cannotApplyConstModifier

What it means

Pkl throws `cannotApplyConstModifier` when a property is declared with the `const` modifier but cannot legally use it. `checkConst` in UnresolvedPropertyNode rejects the declaration when the property overrides a member of a superclass that does not permit a const override (i.e. the modifier cannot be applied to that inherited property). It highlights the `const` token in the module header area of the source.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/member/UnresolvedPropertyNode.java:148

    var isConst = VmModifier.isConst(modifiers);
    if (superProperty.isConst() == isConst) {
      return;
    }
    CompilerDirectives.transferToInterpreter();

    if (superProperty.isConst()) {
      throw exceptionBuilder()
          .withSourceSection(headerSection)
          .evalError(
              "missingConstModifier",
              name,
              superClass.getQualifiedName(),
              sourceSection.getCharacters())
          .build();
    }
    var source = headerSection.getCharacters().toString();
    var constModifierIdx = source.indexOf("const");
    throw exceptionBuilder()
        .withSourceSection(
            headerSection
                .getSource()
                .createSection(headerSection.getCharIndex() + constModifierIdx, 5))
        .evalError("cannotApplyConstModifier", name, superClass.getQualifiedName())
        .build();
  }

  @Override
  public ClassProperty execute(VirtualFrame frame, VmClass clazz) {
    CompilerDirectives.transferToInterpreter();

    var annotations = VmUtils.evaluateAnnotations(frame, annotationNodes);

    var typeNode =
        unresolvedTypeNode == null
            ? null
            : new PropertyTypeNode(

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Remove the `const` modifier from the overriding property declaration
  2. If constant-ness is required, move the property so it is newly declared rather than an override, or change the superclass property's declaration
  3. Check the superclass (named in the message) to see how the property is declared and align modifiers

Example fix

// before (MyModule.pkl overriding base class property)
const name: String
// after
name: String
Defensive patterns

Strategy: validation

Validate before calling

// before running pkl, scan overrides for const modifier
const overridden = /(?m)^\s*const\s+\w+/.test(moduleSource) && moduleSource.includes("extends");
if (overridden) console.warn("const used on override — verify superclass allows it");

Prevention

When it happens

Trigger: Declaring a property as `const` in a class/module that overrides a property inherited from a superclass where applying `const` is disallowed; e.g. overriding an existing property with `const` instead of declaring a fresh one.

Common situations: Refactoring a plain property override into a `const` one assuming const-ness can be added freely; copying `const` modifiers from base classes into subclasses; schema authoring where a base class property is overridden in an extending module.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/648b6ef7789cce5e. Report an issue: GitHub.