Skip to content

[Idea] Bi-directional property accessor #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
EotT123 opened this issue May 30, 2025 · 0 comments
Open

[Idea] Bi-directional property accessor #722

EotT123 opened this issue May 30, 2025 · 0 comments

Comments

@EotT123
Copy link
Contributor

EotT123 commented May 30, 2025

It would be useful to allow method references to properties to be interpreted as both getters and setters. This would enable a single reference to a property to serve as a bi-directional accessor, which could simplify and unify design around property manipulation.

Example:

// Example class with a property
public class Foo{
    @var String bar;
}

// Method accepting a property accessor
public void doSomething(Foo foo, PropertyAccessor<Foo, String> propertyAccessor){
   String bar = propertyAccessor.get(foo);
   ...
   propertyAccessor.set(foo, bar);
}

// Calling the method
doSomthing(new Foo(), Foo::bar);

Do support this behaviour, Manifold would have to replace the method reference for the property with a new PropertyAccessor object, which encapsulates both getter and setter logic for the property.

Here’s a real example from SubTools, where valueGetter and valueSetter are specified separately:

OPTIONS_LANGUAGE(createSettingEnum(Language.class)
	.rootElementFunction(SettingsControl::getSettings)
	.valueGetter(Settings::getLanguage)
	.valueSetter(Settings::setLanguage)
	.defaultValue(Language.ENGLISH)),
OPTIONS_ALWAYS_CONFIRM(createSettingBoolean()
	.rootElementFunction(SettingsControl::getSettings)
	.valueGetter(Settings::isOptionsAlwaysConfirm)
	.valueSetter(Settings::setOptionsAlwaysConfirm)
	.defaultValue(false)),
OPTIONS_CONFIRM_MAPPING(createSettingBoolean()
	.rootElementFunction(SettingsControl::getSettings)
	.valueGetter(Settings::isOptionsConfirmProviderMapping)
	.valueSetter(Settings::setOptionsConfirmProviderMapping)
	.defaultValue(true)),
...

With support for PropertyAccessor, this could be simplified to:

OPTIONS_LANGUAGE(createSettingEnum(Language.class)
	.rootElementFunction(SettingsControl::getSettings)
	.valueAccessor(Settings::language)
	.defaultValue(Language.ENGLISH)),
OPTIONS_ALWAYS_CONFIRM(createSettingBoolean()
	.rootElementFunction(SettingsControl::getSettings)
	.valueAccessor(Settings::optionsAlwaysConfirm)
	.defaultValue(false)),
OPTIONS_CONFIRM_MAPPING(createSettingBoolean()
	.rootElementFunction(SettingsControl::getSettings)
	.valueAccessor(Settings::optionsConfirmProviderMapping)
	.defaultValue(true)),

Support for PropertyAccessor should be limited to fields annotated with @var (excluding those with private @get or @set), or cases where both a public getter and setter method are defined (or a combination).

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

No branches or pull requests

1 participant