Skip to content

Consider per-field wither methods (withXxx(value) → new instance) with configurable enablement #269

Description

@AndreasIgel

Feature Request

Inspired by RecordBuilder's per-field wither pattern, where each field gets a withXxx(value) method that returns a new instance with only that field changed.

Current State

Simple-builder already has a With interface with a consumer-based with(Consumer<Builder>) method that is more flexible but more verbose for single-field changes:

// Current: consumer-based (more flexible)
var modified = original.with(b => b.accreditationName("new value"));

// Proposed: per-field wither (more concise)
var modified = original.withAccreditationName("new value");

Proposed Configuration

Add a tri-state configuration to @SimpleBuilder:

Value Behavior
ENABLE Generate per-field withers for all supported types (records and classes)
RECORDS_ONLY Generate per-field withers only for record types (where implementation is trivial via canonical constructor)
DISABLE (default) Do not generate per-field withers

Why tri-state?

  • RECORDS_ONLY: Records have a canonical constructor making per-field withers trivial and efficient (new Record(field1, newValue, field3, ...))
  • ENABLE: For classes, implementation requires a copy constructor or re-reading all fields — more complex and potentially slower
  • DISABLE: Keeps default behavior unchanged; existing consumer-based With interface already covers this use case

Implementation Notes

  • For records: withXxx(value) => new Record(accessor1(), value, accessor3(), ...)
  • For classes: withXxx(value) => create builder from instance via copy constructor, set field, build
  • Should be generated in the With interface alongside existing with(Consumer) and with() methods
  • Consider method naming convention: withXxx() vs withXxxValue()

Priority

Lower priority than field getters. The existing consumer-based With interface already covers this use case albeit more verbosely.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfuture-ideaLonger-term idea, not scheduled work

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions