Class CaptureConstructorCreator

java.lang.Object
org.ek9lang.compiler.phase3.CaptureConstructorCreator
All Implemented Interfaces:
Consumer<EK9Parser.DynamicClassDeclarationContext>

final class CaptureConstructorCreator extends Object implements Consumer<EK9Parser.DynamicClassDeclarationContext>
Decides THE capture constructor of a dynamic class, creating the sole synthetic constructor when it must.

Phase 2 deliberately does NOT create constructors for dynamic classes. This creator runs in Phase 3 after captures are resolved by DynamicCaptureOrError, and creates either:

  • A no-arg constructor when captures are empty
  • A capture-based constructor when captures are present and the developer has not written one whose parameter types are EXACTLY the capture types - a constructor of the same arity but other types, or one an Any or promotable parameter would accept, is an ordinary constructor and is not it. The capture constructor, written or created, is recorded once (IsCaptureConstructor) for phases 5 and 7.

When the developer has also written another constructor, the captures are then recorded as properties declared with no value (CapturedPropertiesRecorder): only the capture constructor sets them, so every other constructor must, as for a named class.

Only one constructor is created — never both. This ensures that dynamic classes with captured fields cannot be instantiated via a no-arg constructor that would leave those fields uninitialized. Users who want unset fields can pass unset values explicitly (e.g. String(), Integer()).