Skip to content

Fill in DefaultVal fields wrapped in Short, Positional or Description - #21

Open
clockblocker wants to merge 1 commit into
wave-base-3from
wrapped-default-vals
Open

clockblocker wants to merge 1 commit into
wave-base-3from
wrapped-default-vals

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

Makes rfl::DefaultVal<T> work when wrapped inside rfl::Short, rfl::Positional, rfl::Description, rfl::Rename, or rfl::Deprecated (in any order). Previously, readers treated wrapped DefaultVal fields as required while JSON Schema marked them optional without a "default".

Related: comment on munich-data-engineering/reflect-cpp#534.

Example

struct Config {
  rfl::Positional<rfl::DefaultVal<std::string>> input = std::string("in.txt");
  rfl::Short<"o", rfl::DefaultVal<std::string>> output = std::string("out.csv");
  rfl::Short<"j", rfl::Description<"Worker threads", rfl::DefaultVal<int>>> jobs = 4;
};

// Before: error — Field named 'input' not found.
// After:  input="in.txt", output="out.csv", jobs=8
rfl::json::read<Config>(R"({"jobs":8})");

// Before: "jobs" had no "default" in schema
// After:  "jobs": {"type":"integer","description":"Worker threads","default":4}, "required":[]
rfl::json::to_schema<Config>();

Key details

  • internal::is_default_val_v looks through Rename, Short, Positional, Description, and Deprecated in any order.
  • Schema generation emits "default" beneath Description and Deprecated wrappers too.
  • Backwards compatibility: if a struct has only wrapped DefaultVal fields and cannot be default-constructed (or T{} throws), it falls back to main's behavior (fields required) instead of breaking existing code. Bare DefaultVal still static_asserts without a default constructor and errors if T{} throws.
  • Wrapped DefaultVal in rfl::NamedTuple or structs with std::atomic behind smart pointers stays required/compiling as on main.
  • Docs updated in default_val.md and cli.md.

Open question: should wrapped DefaultVal match bare DefaultVal strictly (static_assert without a default constructor, error when T{} throws) instead of falling back? (See comment below.)

AI-authored. Base: main (after munich-data-engineering#786).

@clockblocker

Copy link
Copy Markdown
Owner Author

AI-drafted:

On main, an rfl::DefaultVal wrapped in rfl::Short, rfl::Positional or rfl::Description is ignored, so such a struct never needs a default constructor.

This PR fills those defaults in. To stay compatible, it does so only when T{} exists and doesn't throw. Otherwise the struct reads as on main, with the wrapped fields required.

That is the situation raised in munich-data-engineering#786 (comment): adding a sibling field without a default constructor, or with one that throws, silently changes what the DefaultVal does.

Would you rather a wrapped DefaultVal behave exactly like a bare one (a static_assert without a default constructor, "Could not default-construct" when it throws), at the cost of breaking structs that compile on main today?

Fields such as rfl::Short<"o", rfl::DefaultVal<T>>,
rfl::Positional<rfl::DefaultVal<T>> and
rfl::Description<"...", rfl::DefaultVal<T>> were required by every
reader, rfl::json::read and rfl::cli::read included, while the JSON
schema listed them as optional without a default.

internal::is_default_val_v now looks through Rename, Short, Positional,
Description and Deprecated, in any order, and the schema sets the
default on the DefaultVal node beneath Description and Deprecated.

Compatibility: such a struct is now default-constructed before it is
read, as one with a bare DefaultVal is. If it has no default
constructor, or default construction throws, a struct whose only
DefaultVal fields are wrapped is read as on main, without its defaults,
with those fields required. A wrapped DefaultVal in an rfl::NamedTuple,
or in a struct with atomic fields read through a pointer, stays
required, so such code keeps compiling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant