[Vala] private fields vs bodyless properties



Now the construct keyword for properties in the class constructor
arguments is being deprecated, I started to adjust some things and
stumbled upon some implementation questions.

While "private int foo { get; set; }" does not make much sense and
should better be optimized to "private int foo;", assignment of the
private field foo in the constructor with a constructor argument is not
possible at the moment, only property assignments are allowed. There
might be a good reason for this, but I could not find any reference, so
just a quick question here.

The strange thing is, that whenever "private int foo { get; set; }" is
used as an rvalue it translates to something like self->priv->_foo, but
when I use it as an lvalue the setter method gets invoked, which can
cause overhead in some situations.

After some digging I found that I can also use _foo as property lvalue
in the vala source bypassing the setter, but this feels more like a hack
and im not sure if I can rely on that.

Assigning private or protected values during construction is a rather
common practice, but I don't know what the preferred solution would be.
Personally I like the {get; set;} thing, even for just marking private
fields read/write/construct only, but maybe some final optimization
layer in the generator should simplify this.

Hans




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]