Add "publicread" keyword for class members.
Add a feature that allows public reading of classmembers but only protected/private write access to avoid excessive use of gettermethods which blow up the code.
Solution 1: introduce a new keyword for that purpose:
class MyClass
// missilerange can only be written from inside the class, but read from everywhere:
publicread real missileRange
Solution 2: make writing gettermethods easier by adding some kind of shortcut (exemplary syntax):
class MyClass
private real missileRange : public getMissileRange
compiles into:
class MyClass
private real missileRange
public function getMissileRange() returns real
return missileRange
I currently prefer this solution:
- Add
publicreadkeyword - Allow to write custom getters and setters (just normal get- and set-methods) which can be used like normal member variables.
Yea the keyword is probably the better solution, i agree that the
: public getMissileRange
thing doesnt improve readability.
How about that?
private int _x
property int X
public getter = () -> _x
private setter = (int value) -> _x = value
and readonly property
private int _x
property int X
public getter = () -> _x
or readonly property
private int _x
property int X = () -> _x
I don't think property would be a good keyword and having scopes directly between variables neither.
I think something like properties is necessary in the long run, because otherwise you end up as in Java where you have to make every variable private and use getter and setter methods instead.
But I would not take this C# like syntax. I more prefer the way it is done in xtend, where reading x.foo simply means the same as x.getFoo() if there is no variable x.foo but the respective getter method. This also fits well with the way Wurst does operator-overloading, where we also rely on naming conventions.
sounds good, xtend like syntax it's something like this?
class Person
@property string name // it will be auto-property
@property int age
function setAge(int value)
if value > 0 and value <= 120
_age = value
else
error("Age cannot be lower than 0 or greater than 120")
private function test()
int age = this.age // its getter?
age = this._age // its valid syntax?
Yes, it would be similar to this. I think the @property annotation is not necessary. Xtend probably just has that for interoperability with Java.
I think keyword or annotations needed because I want to see difference between field and property at first sight, especially in the large code
what about this @peq ? IIRC the publicread keyword has existed in the grammar for years now, but is unused.
Planned for 2022 ;)