[Vala] ref in combination with array's and getters in vala



It was by accident that I discovered that ref Complex c = ref arr [i]; is allowed in the vala syntax, while 
it implements a by-reference semantic, in the right-hand side of the assignment. 
Things were different on the left-hand side, where just complex numbers can be passed by value, due to the 
fact that ref Complex c, is not allowed as a local variable in a method body. But ref Complex c is preferred 
when a Complex number is the only semantically useful technique to pass a reference to a struct to a method 
through a variable. A specific way to compute the complex conjugate will work with a ref formal argument, but 
the syntax (parser) makes objections when the variable is a local variable within a method body. The reason 
for this distinction is, for me, difficult to comprehend.

There are clear reasons why one would not allow a ref Complex as a member variable in a class, since that 
would give problems with address-serializations, but I see no objections to ref Complex c; as a member 
variable, used to reference a struct. It is of course possible to resort to the c-language aspects of vala to 
get agrees of reference, that are semantically equivalent. Such c-language references can however not be 
passed to ref arguments. 

It is often the goal to implement a fully orthogonal set of that does not make a distinction to first class 
variables (formal arguments) and inferior ones like local variables. 

It is from a semantic point of view very important to distinguish between a ref variable and a copy of a 
variable, that is allocated on the stack during a function call, this is true for all value types.

I can imagine that a method with a struct as return-value, will just return a ref to the struct as the output 
value, that is used by the caller to copy the referenced value.

This would imply that a getter that should return a struct could pass its result either by reference, like 
this is the case when an element of a complex array is returned, or as a copy of the reference, depending on 
the use of the ref keyword in combination with the receiving variable.

Vale does not support ref local variables in method bodies, but it does support ref in combination with 
formal arguments. This works fine as long as ref arr [i] is used as a formal argument. This fails however 
when syntax support for array acces comes into play. I have the impression that a few special cases that are 
covered by the vala compiler for ref arr [i] are omitted when a getter is used, but it could as well be that 
I have to use code attributes like the (un)owned keyword.

I have hence two questions:

1. Is there a good reason why ref Complex s; local variables are not allowed within method bodies, while this 
is the preferred method to get a specific semantic behavior when methods are to be called?
2. Do I need code attributes, that influence the behavior of the getter that references a struct Complex 
using syntax support?

Thanks in advance for any help.

J.Smit 


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