Re: [Vala] request to add another example to Projects/Vala/ListExample



Hi Gergely,

Yesterday I noticed that Vala 0.32.1 magically compiled my project with a stable memory consumption.

I was going to say "yes, please do add an example!" but then decided to check if Mohan's use case still 
applies.
So I wrote this:

void main ()
{
        Gee.List<string> list0 = new Gee.ArrayList<string>();
        Gee.List<unowned string> list1 = new Gee.ArrayList<unowned string>();
        
        list0.add("helloworld");
        list1.add("helloworld");
        
        print("list0.length="+list0.size.to_string()+",
                list1.length="+list1.size.to_string()+"\n");
        // output: list0.length=1, list1.length=1

        list0.remove("helloworld");
        list1.remove("helloworld");
        
        print("list0.length="+list0.size.to_string()+",
                list1.length="+list1.size.to_string()+"\n");
        // output: list0.length=0, list1.length=0
}
 
So it seems that Vala does a clean job when removing items from a Gee.List<G> now, no matter whether I use an 
unowned type in the generics or not.

Note that I couldn't exactly resemble Mohan's example. I only get to call a ctor of an implemented type (e.g. 
ArrayList, not List), so my example might prove something different in the end. Just in case it's relevant 
for our discussion.


Best,

gilzad




Gesendet: Mittwoch, 22. Juni 2016 um 11:10 Uhr
Von: "Gergely Polonkai" <gergely polonkai eu>
An: "Gilzad Hamuni" <gilli4 gmx net>
Cc: vala <vala-list gnome org>
Betreff: Re: [Vala] request to add another example to Projects/Vala/ListExample

Hello,
 
this is a pretty old topic, but I have the rights to edit wgo pages. If you think it is actually relevant, I 
can add this example to the wiki.
 
Best,
Gergely
 

 

Gergely 
Polonkai[https://about.me/gergely.polonkai?promo=email_sig&utm_source=email_sig&utm_medium=email_sig&utm_campaign=external_links]

about.me/gergely.polonkai[https://about.me/gergely.polonkai?promo=email_sig&utm_source=email_sig&utm_medium=email_sig&utm_campaign=external_links]
 
2016-03-21 15:21 GMT+01:00 Gilzad Hamuni <gilli4 gmx net[gilli4@gmx.net]>:Hi,

just want to say that this is some precious info that I needed to learn yet.

As a C#-guy I wonder from a "user's" point of view:
If List<string> implies strdup(), wouldn't it be consistent to automatically compare with strcmp() upon 
removing, too?

Or am I making dangerous assumptions that are not so smart in the end? I might be spoiled by too much comfort.

gilzad


Gesendet: Freitag, 18. März 2016 um 22:33 Uhr
Von: MohanR

HI,
I screwed-up that comment. sorry for that. I tried to upload a new
version of that page, but wiki says I'm not allowed. Please help to
update that page.
int main(string[] args) {
    List<string> list0 = new List<string>();
    List<unowned string> list1 = new List<unowned string>();
    list0.append("helloworld"); /* g_strdup() takes place */
    list1.append("helloworld"); /* no g_strdup() */
    stdout.printf("list0.length=%u, list1.length=%u\n",
                  list0.length(), /* length=1 */
                  list1.length()); /* length=1 */
    list0.remove("helloworld"); /* wont work as expected */
    list1.remove("helloworld"); /* works as expected */
    stdout.printf("list0.length=%u, list1.length=%u\n",
                  list0.length(), /* still length=1 */
                  list1.length()); /* length=0 */
    list0.delete_link(list0.find_custom("helloworld", strcmp));
    stdout.printf("list0.length=%u, list1.length=%u\n",
                  list0.length(), /* length=0 */
                  list1.length()); /* length=0 */
    return 0;
}
Thanks,
Mohan R
On Fri, 2016-03-18 at 20:37 +0100, Luca Bruno wrote:
Hi,
thanks a lot for your contribution. The wiki is open, you can
register and edit it freely! :)

Also pay attention to the comment about "how" and "how". In C, they
are equivalent because those are const strings and have the same> > address. But with List<string> they 
get copied. However with
List<unowned string> they would be equal. It would be better if you

could reword that comment about pointing to the ownership of the List
elements

On Fri, Mar 18, 2016 at 8:26 PM, MohanR <mohan43u gmail com[mohan43u@gmail.com]> wrote:
Hi,

I'm a beginner in vala. I would like to add the following example
to
this wiki link to make other beginners understand important thing
about
List<string>.

https://wiki.gnome.org/Projects/Vala/ListSample[https://wiki.gnome.org/Projects/Vala/ListSample]

int main(string[] args) {
    List<string> mylist = new List<string>();
    mylist.append("hi");
    mylist.append("how");
    mylist.append("are");
    mylist.append("you");

    /* prints length: 4 */
    stdout.printf("length: %u\n", mylist.length());

    /* following wont work as expected because in
     * C universe, "how" and "how" is not equal
     */
    mylist.remove("how");

    /* still prints length: 4 */
    stdout.printf("length: %u\n", mylist.length());

    /* works because "how" and "how" is equal
     * according to strcmp()
     */
    mylist.remove_link(mylist.find_custom("how", strcmp));

    /* prints length: 3 */
    stdout.printf("length: %u\n", mylist.length());

    return 0;
}
_______________________________________________
vala-list mailing list
vala-list gnome org[vala-list@gnome.org]
https://mail.gnome.org/mailman/listinfo/vala-list[https://mail.gnome.org/mailman/listinfo/vala-list]



-- 
NixOS Linux
_______________________________________________
vala-list mailing list
vala-list gnome org[vala-list@gnome.org]
https://mail.gnome.org/mailman/listinfo/vala-list[https://mail.gnome.org/mailman/listinfo/vala-list]

_______________________________________________
vala-list mailing list
vala-list gnome org[vala-list@gnome.org]
https://mail.gnome.org/mailman/listinfo/vala-list


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