GNOME Bugzilla – Bug 540415
G(S)List find_custom binding too restrictive
Last modified: 2014-01-29 05:23:55 UTC
The g_list_find_custom and g_slist_find_custom bindings currently accept 'G' (the element type of the list) as the query, but since a custom CompareFunc is being applied, it is not necessary to restrict the query to type 'G'. It should just be a void* instead. Patch on its way.
Created attachment 113506 [details] [review] Fixes g_(s)list_find_custom binding
I see that you can theoretically pass a value of a different type to the `data' parameter, however, do we really need this? Can't you just store the value in a prototype object that has the same type as the list elements? The issue with the proposed change is that e.g. int values are not compatible with void*, so you'd break searching for int values. The correct change would probably be something like: public weak SList<G> find_custom<K> (K data, CompareFunc<G,K> func); However, this needs support for generic methods and generic delegates, which we don't have yet, and it might make it more complicated than necessary. Can you provide a concrete use case?
Once generic methods/delegates are supported, the binding probably should be changed as you suggest. (In reply to comment #2) > I see that you can theoretically pass a value of a different type to the `data' > parameter, however, do we really need this? Can't you just store the value in a > prototype object that has the same type as the list elements? > Yes, but I just thought it would be easier this way, in general. It would be a lot of work to design everything to support various prototype forms just to support searching for them in a list. > The issue with the proposed change is that e.g. int values are not compatible > with void*, so you'd break searching for int values. The correct change would > probably be something like: > > public weak SList<G> find_custom<K> (K data, CompareFunc<G,K> func); > > However, this needs support for generic methods and generic delegates, which we > don't have yet, and it might make it more complicated than necessary. > I think that's a good idea once there is support for generic methods and delegates. In the mean time I'll just do something else. > Can you provide a concrete use case? >
I think this is somewhat needed. I for example has a list<Client> clients; Now I want to add a client with uid X. Before adding, I want to check if client X already exists. so I have to find a client with client.uid == X. find_custom is perfectly capable of doing this in C, so why not in vala? My compare function is then something like this: private int custom_client_search(Client a, string uid) { if(a.uid == uid){ return 0; } return 1; }
In my opinion, we should have two bindings for this function to satisfy the two use cases.
commit a4b869a6c5dd7763748acdca837149e924d02385 Author: Evan Nemerson <evan@coeus-group.com> Date: Tue Jan 28 21:22:01 2014 -0800 glib-2.0: add search functions for (S)List and Queue Fixes bug 540415.