Generate get_<property> / set_<property> functions

Having this basic example.

Define the “title” property:

  protected string _title;
  
  public string title { 
  get { return this._title; } 
  set {
      this._title = value;
      title_label.set_label(this._title);
   } 
  }

Works correctly. You can set/get the property by doing

var y = <object>.title;
<object>.title = x;

But is there any way that I also generate get_() and set_property(…) functions?

var y = <object>.get_title();
<object>.set_title(x);

Maybe with some attribute. But I didn’t find a specific one in the list:
https://wiki.gnome.org/Projects/Vala/Manual/Attributes

Thanks!

No, you would need to do that manually. Why do you need that?

Because all other objects (such as GTK) allow getting/setting properties both ways.

Gtk isn’t written in Vala, and traditionally Vala hasn’t been able to tell that title and set_title from C projects mean the same thing — the fact you see both is effectively an accidental implementation detail, that I suspect is still around simply to avoid breaking existing code.

Under-the-hood your object does actually have get_title etc, and if you were to use your object from C you would have to use it, but since your in Vala it’s nicely abstracted away for you.

Yes. I just said GTK as an example that you can change the value by assignment or by a function.

I was curious, I didn’t know that it was “an accidental implementation detail”

It is planned to deprecate all these _get() and _set() methods in gtk since there is the “getter” and “setter” gir annotation to discover these.

https://gitlab.gnome.org/GNOME/vala/-/issues/1212

2 Likes