Genie's writeonly property

81 views Asked by At

I read it from vala tutorial

for readonly: vala

public int b { get; private set; }

in Genie:

prop readonly b: int

for writeonly: Vala:

public int b { private get; set; }

Genie: [this line: syntax error]

prop writeonly b: int

How to declare an one-line writeonly property in Genie? maybe something like?

prop XXX b: int

We can write a FOUR lines writeonly property:

class Wonly
    _b: int
    prop b: int
        set
            _b = value

init
    var w = new Wonly
    // print w.b // ERROR!! writeonly!!
    w.b = 456   // OK

but How to write an one-line writeonly property?

1

There are 1 answers

1
lf_araujo On

I will do my best to answer your question here, however some aspects of your question are not clear. Maybe it would be help to give a bit more context, explaining better what you are planning to achieve and a little bit more of code for contextualization.

That said, I will assume you are asking about the syntax for class properties in Genie.

Properties are ways of hiding the implementation details from the users of the class you developed. According to Vala's tutorial this move is also called the information hiding principle in computer science.

In Vala, a propertie would be defined within a in the following way:

static int current_year = 2525;
class Person : Object {
    private int year_of_birth = 2493;

    public int age {
        get { return current_year - year_of_birth; }
        set { year_of_birth = current_year - value; }
    }
}

In Genie, it would look like this:

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]    
    prop output : string
        get
            return "output"
        set
            _name = value

Now for the write only properties. It is a bit of a controversial matter based on this and this questions at SO. It seems to be useful only when you are not planning to read what you write. But as you can see on the questions above, most of the answers suggest the creation of methods instead of using write only properties.

That takes us to the syntax you are pointing to:

public int b { private get; set; } 

You state that this is the syntax to a write only property in Vala, and it seems to be true. This is because, by setting the get as private you prevent the user to read the value. You can make the get or set as private in Vala by leaving it out of the set block as well, ie, in Vala you could simply remove the private get part.

Now this is where I am unsure, but I suggest you try it out in your code. Based on Vala's ability to set private getters or setters by removing them from the set block, I suspect that the same applies to Genie.

I removed the setting of the get from the following code and it compiled:

[indent=4]

class Foo : Object

    prop name : string

    prop readonly count : int

    [Description(nick="output property", blurb="This is the output property of the Foo class")]
    prop output : string
        set
            _name = value

init
    var foo = new Foo()

Maybe that is what you are looking for, but I am not sure it would work in real code. If it does not, perhaps you would be better off with methods instead.