So this is what I now have: namespace ObjectFactory {
[CCode (has_target = false)] public delegate Object Maker(); Gee.HashMap<Type,Maker> construct_map; public void add_maker(Type for_type, Maker maker) { if (construct_map == null) { construct_map = new Gee.HashMap<Type,Maker>(); } construct_map[for_type] = maker; } public Object? make(Type one_of_these) { if (construct_map != null && construct_map.has_key (one_of_these)) { Maker m = construct_map[one_of_these]; return m (); } return null; } } and then in each instantiation: public class IntArr : GArrSerializable<int> { class construct { ObjectFactory.add_maker(typeof (IntArr), () => { return new IntArr (); }); } public IntArr(int size = 0) { base (size); } } then I use ObjectFactory.make (type) in my deserialization procedure, and it appears to work ok at last. On Wed, Feb 17, 2016 at 10:26 AM, Andy Lees <andrewl...@gmail.com> wrote: > Momentarily I thought that might work, but it does match the Json > serialization structure - the context of deserializing an array does not > provide the initialised array, the deserialization procedure has to do the > creation, and only has the type id to work with. It looks like I'll have > to create an object factory of my own for this, as I don't want to get into > the internals of Json glib. > > On Wed, Feb 17, 2016 at 10:09 AM, Daniel Espinosa <eso...@gmail.com> > wrote: > >> I run in the same issue for GXml and SerializableListArray<>, for example. >> >> The way I resolve, in my case, I've defined a new Interface called >> SerializableCollection, with a init() function. This function is called by >> Serializable.deserialize() method in order to help implementators to >> initialize its generic containers by calling MyObjectCollection.new() >> providing any information it needs like the Generic Type it should work on. >> >> In my other project, I've just initialize any public property as >> >> MyProperty<int> prop { get; set; default = new MyProperty<int>(); } >> >> or inside construct {} >> >> The point is to call MyProperty.new() not Object.new() on generic classes. >> >> >> 2016-02-16 16:59 GMT-06:00 Andy Lees <andrewl...@gmail.com>: >> >>> Yes, the problem is that for types derived from generics (and for the >>> generic constructor itself), the type information is not set in the >>> construct function, so very little can be done. >>> >>> On Wed, Feb 17, 2016 at 9:53 AM, Daniel Espinosa <eso...@gmail.com> >>> wrote: >>> >>>> 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...@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 <eso...@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...@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 <eso...@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...@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) >>>> >>> >>> >> >> >> -- >> 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) >> > > _______________________________________________ vala-list mailing list vala-list@gnome.org https://mail.gnome.org/mailman/listinfo/vala-list