Re: [Vala] How to check if GLib.List is empty?



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2/12/2014 16:34, Виталий Кирсанов wrote:
Hm, indeed. The following piece of code works fine:

int main(string[] argv) { List<string> list = new List<string>(); 
assert( list == null ); return 0; }


So this seems to be the answer to my question. But still there are
two weird things in my opinion:


Hmm, although it answers your question I still think this is wrong in
the language.

For me new means that a heap allocation took place. So if it's null
then that means that the error handling for in case memory allocation
failed should be used. Not a non-failure use-case such as yours
(checking size of the list).


This should in my opinion just work (Vala should deal with list being
NULL in its generated code when calling .count on it).

int main(string[] argv)
{
    List<string> list = new List<string>();

    if ( list == null ) {
        print ("Can't allocate sufficient memory");
        return -1;
    }

    assert ( list.count() == 0 );

    return 0;
}

I guess it could do this by implementing list_count (List *list) like
this:

guint list_count (List *list)
{
        GList *elem;
        guint cnt = 0;

        if (list == NULL)
                return 0;

        for (elem=list; elem; elem=elem->next)
                cnt++;

        return cnt;
}

I guess the ==null operator should just always return false for List
(as for a new empty list nothing gets allocated, so only prepend and
append could return memory allocation failure as error condition).

Gee.List is indeed probably better to use instead.

kind regards,

Philip


- Why I need to call operator new if the result value is still
null? Can't I simply declare a reference List<string> list; and
then call e.g. append()? - I thought that only references appended
with question mark sign (e.g. List<string>?) can be null, cant'
they?




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (MingW32)

iQEcBAEBAgAGBQJUprWjAAoJEEP2NSGEz4aDpuAIAMTc0eRJ4Zoo12fRR4IgQ0wF
60crZ+gUvyQoRZ0XFwNLS14ij2VXF2+h6xHNeK5RoL6cvzlvzm1QeFwu7jisWd8h
yEJZOS0Nm6jBFN8La0QS1okIjv7AvB7v6pZyR1TBKVd6ZEHfeXZK4H2Dclw7Yqrw
2AEgQ7/+WQOxFCGCmMj35MWhoUy+i7ot0M2diMhRn3AoMja8HcKL942E4GIw19NC
PhKClA6iGlWRHpzcJ3EO2OmNHk8ri+Z23ByIi4BccqRAj8i7jUqU/lCS49p0V02R
4ojXgABmkhuGpLuBn7eDxU2/nXod7890VbYK0iz5w/RK8zdKiX5D8Ujn3maAJ/E=
=nTIr
-----END PGP SIGNATURE-----


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