Re: [Vala] idiomatic way to pass an array for modification to a function



Thanks Felipe very much going to the trouble of
being so explicit and writing out this code in great detail.

This does indeed compile and run on ubuntu 15.10 with vala 0.28.1.

And 'owned' indeed makes it local so that the effect is invisible to the caller.

What i had erroneously done before was make the signature
    "int w[]"
instead offor being
    "int[] w"

(Getting this sort of declaration right is sometimes elusive to me.)

And this is certainly better than the pointer form that i originally had, since
as you say, the "int[] w" form carries length information, so that does
not get separated from the array.

dan

On Mon, Dec 28, 2015 at 5:11 AM, Felipe Lavratti <felipelav gmail com> wrote:
(1)
Copies are only made to owned variables, this is how you know that
copy will implicit made: assigned to a owned variable.
https://wiki.gnome.org/Projects/Vala/Tutorial#Ownership

I see what you are missing, a clear distinction to ownership of
parameter being passed, Vala does that for you: ownership is never
passed, unless otherwise noted and, in most of the cases, parameters
are references.

In vala ownership is defined with 'owned' and 'unowned' qualifiers,
note that functions parameters are 'unowned' by default and variable
declarations are 'owned' by default. Have a read at the link.

I believe that the best signature to your `change_values` function is
`void change_values(int[] w,int loc);` because:
 - I does states that no copy is made, so reference is used (no owned
to parameter `w`)
 - I does expects a buffer that contains the `.length` field for the
parameter `w`.
 - I does states that the buffer can or cannot be changed. (this is
default to vala, there's no way to tell that a parameter will not be
changed in the signature in vala, if it was C, you would use const)
 - `w.length` is available within the function, so size checks can be made.

(2)
The code below builds fine in Vala 0.28.1 & Gnome Ubuntu 15.10

```
void main( ) {
        var t = new int[6];
        change_values(t, 0);
        change_values(t, 3);
        for (var i=0; i<t.length; i++)
                stdout.printf("%d -> %d\n",i,t[i]);
}

void change_values(owned int[] w, int loc) {

        if (loc + 3 > w.length) {
            stdout.printf("Buffer too small.\n");
            return;
        }

        w[loc+0]=1;
        w[loc+1]=2;
        w[loc+2]=3;
}
```


On Mon, Dec 28, 2015 at 12:56 AM, Dan Hitt <dan hitt gmail com> wrote:
Thanks Felipe for your reply.

So two questions:

(1) If i do not want to make a copy, is there a better way to write the
change_values() function?  For example, is there some way that
i could or should modify the change_values() function so that
it does not use pointers?

(2) If i modify change_values to be
void change_values(owned int[] w,int loc) { w[loc+0]=1; w[loc+1]=2;
w[loc+2]=3; }
without making changes in the caller, i get some warnings and
errors from the c-compiler (although not from valac).
Is this expected?  I do understand that i wouldn't want to use
'owned', but i was trying it out just to see what would happen.

Note that i'm very naive and literal about this, so there may be some
obvious context that i'm ignoring.

dan





On Sun, Dec 27, 2015 at 3:20 PM, Felipe Lavratti <felipelav gmail com> wrote:
Hi,

Everything is a reference in Vala, so, there's no buffer copy in your
example.

The rationale is opposite: Everything is passed as reference, if you want to
pass as copy, you have to set the parameter `owned`

```
void change_values(owned int[] w,int loc) { w[loc+0]=1; w[loc+1]=2;
w[loc+2]=3; }
```

Also, since you created an array with `new int[]` you should have an
parameter with the same construction, so that you variable `w` inside
`change_values` will have properties such as `.length`.

On Sun, Dec 27, 2015 at 6:24 PM Dan Hitt <dan hitt gmail com> wrote:

I've been struggling to write a function that can modify an array it
receives as an argument.

Now, as it happens, it looks like you can just write it like it were
C, using pointers.

So this code works and does what i expect:

    void main( ) {
            var t = new int[6];
            change_values(t, 0);
            change_values(t, 3);
            for (var i=0; i<t.length; i++)
                    stdout.printf("%d -> %d\n",i,t[i]);
    }

    void change_values(int *w,int loc) {
            w[loc+0]=1;
            w[loc+1]=2;
            w[loc+2]=3;
    }

However, it doesn't use 'ref' and i would like to write something more
idiomatic.

The constraint is that i really do want to have reference-type semantics
with
no unnecessary copying because the actual application will involve a
routine
called many times on a long input array.

Thanks in advance for any clues on how to make this more stylistic, or any
good
but detailed references.  (This subject seems to come up from time to
time, but the
answers are never quite detailed enough for me to comprehend.)

dan
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list



--
Skype: felipeanl


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