Re: [Vala] Problem with programmatic generic object instantiation



If you want to execute code on Object creation using Object.new(), you
should use

construct {
  // Your code here
}

When define code at MyObject.new(), you execute the code in this method not
Object.new(), then use the above should help.


2016-02-16 15:58 GMT-06:00 Andy Lees <andrewl oz gmail com>:

The problem with Object.new is that it doesn't call the object constructor
so far as I can tell, just calling g_object_new without any of the Vala
object construction code.

My Serializable is an interface.  I'm trying to make use of the Json
serialization framework, and for the most part it's working.  The problem
I'm having is with the construction of instances of array types - there
appears to be no factory framework for object creation in Vala, and so far
the only workaround I've come up with is very clunky.

Is there an object factory somewhere that someone could point me towards?

On Wed, Feb 17, 2016 at 1:49 AM, Daniel Espinosa <esodan gmail com> wrote:

Try use Object.new(typeof(IntArr))

Try to rename type() to any other, to avoid conflics with Object.type()

You are trying to implement Gee.ArrayList<>, may you:

A) wrap Gee.ArrayList in your class

B) Derive directly from Gee.Arraylist then convert your base class
Serializable as an interface with all required methods implemented as
virtual, then you have two bases.

You may want to check at GXml.Serializable interface and its
GXml.SerializableArrayList implementation based on Gee.
El feb. 16, 2016 7:10 AM, "Andy Lees" <andrewl oz gmail com> escribió:

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






-- 
Trabajar, la mejor arma para tu superación
"de grano en grano, se hace la arena" (R) (en trámite, pero para los
cuates: LIBRE)


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