Skip to content

Fix .unset*() for modifiable with primitive field and default value.#1606

Merged
elucash merged 1 commit intoimmutables:masterfrom
aquariusrick:unset-with-primitive-default
Oct 22, 2025
Merged

Fix .unset*() for modifiable with primitive field and default value.#1606
elucash merged 1 commit intoimmutables:masterfrom
aquariusrick:unset-with-primitive-default

Conversation

@aquariusrick
Copy link
Copy Markdown
Contributor

Summary

Ensure modifiable with primitive field and default value returns the default value
and .*IsSet() returns false after .unset*() is called.

Description:

Currently a field such as:

@Value.Modifiable
interface Standalone {

  @Value.Default
  default int def() {
    return 1;
  }
}

Would generate the following for the .unset*() method:
e.g.

/**
 * Reset an attribute to its initial value.
 * @return {@code this} for use in a chained invocation
 */
@CanIgnoreReturnValue
public final ModifiableStandalone unsetDef() {
  optBits |= 0;
  def = 0;
  return this;
}

Having optBits |= 0; does nothing, and doesn't effectively unset the field. Thus, subsequent calls to .def() would return 0, instead of the value returned by the method annotated with @Value.Default (i.e. 1). Further, subsequent calls to .defIsSet() would return true when it should return false.

This change would instead generate the following for the .unset*() method:
e.g.

/**
 * Reset an attribute to its initial value.
 * @return {@code this} for use in a chained invocation
 */
@CanIgnoreReturnValue
public final ModifiableStandalone unsetDef() {
  optBits &= ~OPT_BIT_DEF;
  def = 0;
  return this;
}

Having optBits &= ~OPT_BIT_DEF will cause subsequent calls to .defIsSet() to return false, and calls to .def() to invoke the method annotated with @Value.Default to get the value.

…default value and `.*IsSet()` returns `false` after `.unset*()` is called.
@elucash
Copy link
Copy Markdown
Member

elucash commented Oct 22, 2025

Thank you for PR! this is nice finding, definitely has to be fixed. Merging

@elucash elucash merged commit f7282d5 into immutables:master Oct 22, 2025
16 checks passed
@aquariusrick aquariusrick deleted the unset-with-primitive-default branch October 22, 2025 21:30
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