Does anyone know where to submit a language feature request? I have an idea for a feature, but don’t know where to submit it. Here is the feature. Let me know what you think.
Use annotations to declare private fields as properties. The compiler would detect this and:
- allow direct assignment to and reading of the field.
- or if a get/set is defined for the field it would automatically call the get/set.
To the developer using the class there would be no difference. You just use normal operations as if the field was public. Also the compiler would only call a get or set if one is defined, otherwise direct assignment would be used.
Advantages:
- Remove overhead of a method when it is not necessary while maintaining encapsulation
- Allow more natural use of fields in any type of equation
- No more arguments over public versus private
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
}
public class Bar() {
public void useFoo(Foo foo) {
int id = foo.id;
float tax = 0.10 * foo.cost;
foo.cost = foo.cost + tax;
}
}
Later you could change Foo with no need to change Bar.
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
public float getCost() {
float result = currencyConversionOut(cost);
return result;
}
public void setCost(float cost) {
this.cost = currencyConversionIn(cost);
}
}
