Re: [Vala] Ownership syntax changes



On Sat, 2008-12-20 at 14:28 -0500, Yu Feng wrote:
On Sat, 2008-12-20 at 13:04 +0100, Jürg Billeter wrote:
On Sat, 2008-12-20 at 12:19 +0100, Xavier Bestel wrote:
Le vendredi 19 décembre 2008 à 12:10 +0100, Jürg Billeter a écrit :
I just committed some ownership syntax changes to trunk. The old syntax
will continue to be supported but is planned to be deprecated after the
release of Vala 0.5.4.


      * `owned' type modifier replaces `#' type modifier

This is not often used, but `#' was very unintuitive as a type modifier,
and I therefore decided to replace it by the hopefully better
understandable `owned' modifier. Example of new syntax:

public void foo (owned string bar) {
    [...]
}

I like the idea, but the word "owned" doesn't really tells by who it's
owned (caller or callee). How about something like "callers" or
"transfer" ?

`owned' applied to a parameter means that the parameter owns the object,
that is, the ownership of the object is transferred in the same
direction as the parameter value: to the callee for input paramters, to
the caller for output parameters, and input to the callee and output to
the caller for `ref' parameters.
`owned' is still not much better than # because the world owned doesn't
describe the flow of the ownership; and where people can easily get
confused is the direction of the flow.

This surprises me a bit, I don't see how you can get confused about the
direction of the ownership transfer. Ownership is always transferred in
the same direction as the value is transferred and the direction of the
value should be obvious for input and output parameters and return
values. Do you have an example where the flow of ownership may be
confusing?

So if we do not need to describe the direction of the ownership
transfer, the only question that the type modifiers have to answer is
whether ownership is transferred or not. My proposed terms, `owned' and
`unowned', should make it clear when ownership is transferred.

I read into glib again and perhaps a steal/take pair can work together.

void foo(take string bar); foo will take the ownership of bar from the
caller context.

foo(steal "bar"); the ownership of "bar" is stolen from the caller
context.

(notice the steal now is written in the caller code.)

These are keywords that will only work for parameters and not for fields
or local variables. This means that we need to have separate keywords
for fields and parameters. In my opinion, this would make it more
complicated instead of simpler.

Please note that it should rarely be necessary to use `owned' and
`unowned' in Vala code. They will mostly be used in bindings, so I think
it's ok if they are a bit more technical than other syntax parts.

Jürg





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