Re: [Vala] Problem with programmatic generic object instantiation



I would have expected the generic type to be obtained from the type id of
the derived type (generic instantiation), which is obtained fresh each
execution, but it seems not.  Is the generic type a hidden property of the
class derived from the generic class, and if so, what would its name be so
that it can be set in the new call?

Alternatively, is there a way to call an object constructor given the type
id?

On Tue, Feb 16, 2016 at 11:16 PM, Daniel Espinosa <esodan gmail com> wrote:

Take in account that Type system is dynamically created, then a type
number will change with each execution.

Use

YourCreatedObject is typeof (Object)

to check if the created object is of type you need, like a derived object.

You can use

obj.get_type().is_a(typeof (Object))

And consider that your example is exactly the way to create an object of
Generic class, because you have set tje object type. Create a generic
instance with Object.new you should provide the object type.
El feb. 16, 2016 5:14 AM, "Andy Lees" <andrewl oz gmail com> escribió:

Hi,

I have a generic array implementation like so:

public class GArrSerializable<T> : Serializable, Object  {
  public T[] data;
  public int length;
  public GArrSerializable(int size = 0) {
    data = new T[size];
  }

  public Type type () { return typeof (T); }

...etc

and a derived type:

public class IntArr : GArrSerializable<int> {

  public IntArr(int size = 0) {
    base (size);
  }
}

If I create an instance of the type, then instance.type () returns 24 as
expected.
If I create an instance like so:
var ia = Object  new (type) as IntArr;
var t = ia.type ();

Then t is 4 rather than 24.  As it is in the instance when it tries to
deserialize.

Can you tell me why this is so, and how I can programmatically create
objects derived from a parameterised type?

Thanks

Andrew Lees
_______________________________________________
vala-list mailing list
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]