Search code examples
pointersvala

Accessing pointer object properties in Vala


I have refactored a bit my code, so i need a pointer that could contain multiple liste types :

owl_list = new Gee.LinkedList<OpenWithAction> ();
a_list = new Gee.LinkedList<OpenAppAction> ();

Gee.List* any_list = null;

So i have a pointer any_list which i can use to access either owl_list or a_list (depending on a switch no present here, but assume there is)

if (!any_list.size)
    return null;

But this will fail as valac throws at me error: The name `size' does not exist in the context of `Gee.List*' if (!any_list.size)

I haven't done any C,C++ since a very long time and I'm no vala expert, since i use more typeless languages, but is there any way this could work ?

EDIT:

I just tried

fieldType = OpenWithAction.get_type();
if (!(any_list as Gee.List<fieldType>).size)

error: The type name `fieldType' could not be found if (!(any_list as Gee.List<fieldType>).size) Obviously I'm doing something wrong, what i'm trying to do is : Vala: determine generic type inside List at runtime, i just can"t manage to implement it. return null;

EDIT 2:

I just solved partially my problem :

As said by @jens-mühlenhoff, yes OpenWithAction and OpenAppAction have a common ancestor and it is GLib.Action

So all i had to do is declare :

Gee.List<Action> any_list = null;

instead of

Gee.List* any_list = null;

now foreach (var action in any_list) is working, but i'm still getting an error with

if (any_list->size == null)
    return null;

error: The name `size' does not exist in the context of `Gee.List<Synapse.Action>?' if (any_list->size == null)

another try is :

if (!any_list.size)
    return null;

Operator not supported for `int' if (!any_list.size)


Solution

  • Two things i was doing wrong :

    1- Like in C# i should not have used a pointer if not needed. So using : Gee.List<Action> any_list = null; is just working fine. The explanation is that OpenWithAction and OpenAppAction have a common ancestor class, So declaring a List with that type is just working fine ( on the other hand i don't know in the case they didn't have a common ancestor).

    2- int type can't be used as bool type so if (any_list.size == 0) would work whereas if (!any_list.size) would throw an error

    Thanks every one for helping :)