Search code examples
valadesign-by-contract

Requires on setter/getter in vala


Is there any way to do something like the following in Vala ?

public int year {
    get { return this.year; }
    set requires (1500 < value && value < 2050) { this.year = value; }
}

Solution

  • No, but 'requires' is really just syntactic sugar for GLib.return_if_fail and GLib.return_val_if_fail, so you could just do:

    public int year {
        get { return this.year; }
        set {
            GLib.return_if_fail (1500 < value && value < 2050);
            this.year = value;
        }
    }