Re: [Vala] Question regarding passing a strings as arguments to a method



Hello,


On 11 يول, 2011 م 05:15, Serge Hulne wrote:
All right,

But it is a rather odd class indeed then, because:

1) I dont't think it has the value "null" when unassigned.
It does (or at least it should).


2) It derogates to the rule according to which the default behaviour in
an assignment between class types is by reference (when a string is
assigned to another, the default is to copy the data, unless it is
explicitly declared "unowned")

Not really: the default behaviour in an assignment is to either increment the refcount or copy (depending on whether the class is refcounted or not), except that if the class isn't refcounted and isn't immutable (GLib.List for example) you'll need to explicitly copy it. But since string is immutable (and this is where it is special, though you can make your class behave so), it will be implicitly copied.

3) When a string is created, it does not require the use of the keyword
"new"
( it is: string s = "hello";  not: string s = new string("hello"); )
This is just because of the existence of string literals. If there were list literals in vala, you could create a list without using the new keyword. (I think there are list literals in the dova profile).

4) Yet, it is passed by reference in a function call.
Like any other class ;-)


Since the string class, seems to behave unlike any other class, perhaps
its singular behaviour ought to be emphasized in the documentation.

So yes, it is a bit unusual, but once you understand how vala works, you'll find it more logical.

HTH,
Abderrahim



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