apple/pkl · error · VmTypeMismatchException

type mismatch: value is not of type Dynamic

Error message

type mismatch: value is not of type Dynamic

What it means

Pkl `Dynamic` type check failure. The declared type was `Dynamic` (the type of objects built with `new Dynamic {}`), and the runtime value was not a VmDynamic — e.g. a typed class instance, Listing, Mapping, or a primitive. Note this is an exact-class check, not a subclass check: even class instances fail.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java:677

      return other instanceof TypedTypeNode;
    }

    @Override
    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
      return consumer.accept(this);
    }
  }

  public static final class DynamicTypeNode extends ObjectSlotTypeNode {
    public DynamicTypeNode(SourceSection sourceSection) {
      super(sourceSection);
    }

    @Override
    protected Object executeLazily(VirtualFrame frame, Object value) {
      if (value instanceof VmDynamic) return value;

      throw typeMismatch(value, BaseModule.getDynamicClass());
    }

    @Override
    public VmClass getVmClass() {
      return BaseModule.getDynamicClass();
    }

    @Override
    public Object createDefaultValue(
        VirtualFrame frame,
        VmLanguage language,
        SourceSection headerSection,
        String qualifiedName) {

      return VmDynamic.empty();
    }

    @Override

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Use `new Dynamic { ... }` to build the value so its runtime type is actually Dynamic.
  2. If a typed instance is what you have, change the declared type to the concrete class or `Typed`/`Object`.
  3. Remember Listing/Mapping are not Dynamic; declare the property as `Listing`/`Mapping` instead.
  4. Wrap primitives directly without a Dynamic type — declare the property as the primitive type.

Example fix

// before
conf: Dynamic = new Server { host = "localhost" }
// after
conf: Dynamic = new Dynamic { host = "localhost" }
Defensive patterns

Strategy: type-guard

Validate before calling

// Pkl
if (value is Dynamic) {
  conf: Dynamic = value
}

Type guard

function isDynamic(value: Any): Boolean = value is Dynamic

Try / catch

// Guard with `is Dynamic`; note it is an exact-kind check — class instances fail.

Prevention

When it happens

Trigger: Assigning a `new SomeClass {...}` instance, a Listing/Mapping, Int/String/Boolean, or null to a property typed `Dynamic`; passing such values to an API parameter declared `Dynamic`.

Common situations: Assuming `Dynamic` is a supertype of all objects (in Pkl it is a specific object kind); wrapping typed objects in configs that declare Dynamic; returning a class instance from a function typed to return Dynamic.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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