WurstScript icon indicating copy to clipboard operation
WurstScript copied to clipboard

Add "publicread" keyword for class members.

Open muzzel opened this issue 11 years ago • 10 comments

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

muzzel avatar Mar 10 '14 21:03 muzzel

I currently prefer this solution:

  • Add publicread keyword
  • Allow to write custom getters and setters (just normal get- and set-methods) which can be used like normal member variables.

peq avatar Mar 10 '14 21:03 peq

Yea the keyword is probably the better solution, i agree that the

 : public getMissileRange

thing doesnt improve readability.

muzzel avatar Mar 10 '14 23:03 muzzel

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

AlexPutin avatar Jun 28 '14 16:06 AlexPutin

I don't think property would be a good keyword and having scopes directly between variables neither.

Frotty avatar Jun 28 '14 16:06 Frotty

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.

peq avatar Jun 28 '14 19:06 peq

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?

AlexPutin avatar Jun 28 '14 20:06 AlexPutin

Yes, it would be similar to this. I think the @property annotation is not necessary. Xtend probably just has that for interoperability with Java.

peq avatar Jun 28 '14 20:06 peq

I think keyword or annotations needed because I want to see difference between field and property at first sight, especially in the large code

AlexPutin avatar Jun 28 '14 20:06 AlexPutin

what about this @peq ? IIRC the publicread keyword has existed in the grammar for years now, but is unused.

Frotty avatar Dec 03 '18 09:12 Frotty

Planned for 2022 ;)

peq avatar Dec 04 '18 20:12 peq