Skip to content

optionals with new_union default to NULL#2

Merged
egenn merged 7 commits into
mainfrom
devel
May 6, 2026
Merged

optionals with new_union default to NULL#2
egenn merged 7 commits into
mainfrom
devel

Conversation

@egenn
Copy link
Copy Markdown
Member

@egenn egenn commented May 1, 2026

No description provided.

Copilot AI review requested due to automatic review settings May 1, 2026 15:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the package’s S7 property helpers so that “optional” properties rely on new_union(NULL, <type>) (and no longer an explicit default = NULL) and tightens nullable handling in factory validators. It also adds specs/ to git/build ignore lists.

Changes:

  • Reordered new_union() arguments for many optional_* properties to put NULL first and removed explicit default = NULL.
  • Updated bounded_double_property() and enum() validators to only treat NULL as valid when nullable = TRUE.
  • Ignored specs/ in .gitignore and .Rbuildignore.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
R/00_S7_properties.R Adjusts optional-property union ordering/defaulting and refines nullable validation in factory properties.
.gitignore Ignores specs/ in git.
.Rbuildignore Excludes specs/ from R package builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/00_S7_properties.R
Comment on lines 52 to 54
optional_character_scalar <- new_property(
class = new_union(class_character, NULL),
default = NULL,
class = new_union(NULL, class_character),
validator = function(value) {
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests don’t currently assert the new intended behavior that these optional_* properties can be omitted and will default to NULL (now relying on new_union(NULL, ...) rather than an explicit default = NULL). Please add a test that constructs a class with one of these properties and verifies the field is NULL when not provided (and that construction succeeds).

Copilot uses AI. Check for mistakes.
Comment thread R/00_S7_properties.R
Comment on lines 671 to 677
enum <- function(values, default = NULL, nullable = FALSE) {
cls <- if (nullable) new_union(class_character, NULL) else class_character
new_property(
class = cls,
validator = function(value) {
if (is.null(value)) {
if (nullable && is.null(value)) {
return(NULL)
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum() defaults default = NULL while also defaulting nullable = FALSE. With the updated validator, NULL will now be rejected unless nullable = TRUE, which can make enum(values) produce a property whose default (NULL) is invalid and may break class construction when the field is omitted. Consider either (1) treating default = NULL as “no default” (don’t pass default to new_property() unless non-NULL), (2) automatically setting nullable = TRUE when default is NULL, or (3) choosing a non-NULL default (e.g., first element of values) and documenting it. Add a test covering the chosen behavior.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates optional property definitions in R/00_S7_properties.R by reordering new_union arguments to prioritize NULL as the default value and removes redundant explicit default assignments. It also updates .gitignore and .Rbuildignore to exclude the specs directory. Feedback suggests extending this pattern to bounded_double_property, enum, and the optional() helper function to ensure consistency and prevent potential validation errors where default numeric values might fall outside specified bounds.

Comment thread R/00_S7_properties.R
Comment on lines +641 to 643
if (nullable && is.null(value)) {
return(NULL)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While updating the validator to check nullable, the class definition for cls (at line 636, currently context) should also be updated to new_union(NULL, class_double) when nullable is TRUE.

Currently, cls is new_union(class_double, NULL), which means the property will default to the default of class_double (likely 0.0 or numeric(0)). If this default value falls outside the specified bounds (e.g., if lower > 0), new_property() will throw an error during construction because the default value fails validation. Swapping the order ensures the default is NULL, consistent with the other optional properties in this PR.

Comment thread R/00_S7_properties.R
Comment on lines +676 to 678
if (nullable && is.null(value)) {
return(NULL)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the other changes in this PR, consider updating the class definition for cls (at line 672, currently context) to new_union(NULL, class_character) when nullable is TRUE. Although enum explicitly sets the default argument in new_property(), using the same union order as other optional properties improves maintainability and follows the pattern established in this PR. Additionally, the optional() helper function at the end of the file (line 717) should likely be updated to S7::new_union(NULL, type) for the same reason.

@egenn egenn merged commit 4326aef into main May 6, 2026
1 check passed
@egenn egenn deleted the devel branch May 7, 2026 11:07
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.

2 participants