Re: [Vala] Create an instance of a derived class, from a base class method

2012-02-19 Thread Tal Hadad

I don't understand what is your purpose:
I don't understand why you don't use constructors, or static methods.

See, the type deceleration must be explicit at complication time.
However, if you don't want it, you can always use generics and the dynamic 
word.

In simple words, as far as I know, what you want to achieve isn't possible(but 
could be manually implicated).

Yours,
Tal

 Date: Sat, 18 Feb 2012 15:51:55 +
 From: blowb...@gmail.com
 To: vala-list@gnome.org
 Subject: [Vala] Create an instance of a derived class,from a base 
 class method
 
 Hi,
 
 I am trying to craft a base class method, that is able to create an
 object of the same class as whatever derived-class object it was
 called on.
 
 For example, if classes Bar and Baz both derive from Foo, and Foo has
 a create() method, then bar.create() should give me an instance of
 Bar, and Baz.create() should give me an instance of Baz.
 
 I've spent so long happily experimenting with this that I forgot the
 original requirement. but I have come up with two approaches that
 almost work (below).
 
 Everything is fine at run-time, but I have to resort to ugly casts
 like Derived obj = derived.foo() as Derived to get valac to accept
 it.
 
 I think the return type ('Base') of foo() is my problem, but I'm at a
 loss as to what to replace it with to make this work.
 
 Is there a better way to achieve this sort of thing?
 
 
 
 
 public class Base : GLib.Object {
 public Base foo() {
 Type t = this.get_type();
 return Object.new(t) as Base;
 }
 }
 
 public class Derived : Base {
 public void test() {
 stdout.printf(I am an instance of Derived!\n);
 }
 }
 
 //
 
 /* second attempt with abstract methods */
 public abstract class Base2 : GLib.Object {
 public Base2 foo() {
 return new_instance();
 }
 protected abstract Base2 new_instance();
 
 }
 
 public class Derived2 : Base2 {
 
 /* return type has to be Base2 or vala won't allow the override */
 public override Base2 new_instance() {
 return new Derived2();
 }
 
 public void test() {
 stdout.printf(Derived2: I am an instance of Derived2!\n);
 }
 }
 
 
 //
 
 
 public static int main(string[] args) {
 
 #if false
 Derived derived = new Derived();
 Derived obj = derived.foo(); // 'cannot convert from Base to Derived?'
 #else
 Derived derived = new Derived();
 Derived obj = derived.foo() as Derived; // ugly!
 #endif
 
 Type t = obj.get_type();
 stdout.printf(Type of obj is %s\n, t.name());
 obj.test();
 
 
 /* second attempt with abstract methods */
 #if false
 Derived2 derived2 = new Derived2();
 Derived2 obj2 = derived2.foo(); // 'cannot convert from Base to Derived?'
 #else
 Derived2 derived2 = new Derived2();
 Derived2 obj2 = derived2.foo() as Derived2; // ugly!
 #endif
 
 Type t2 = obj2.get_type();
 stdout.printf(Type of obj2 is %s\n, t2.name());
 obj2.test();
 
 
 return 0;
 }
 
 
 cheers
 ant
 ___
 vala-list mailing list
 vala-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/vala-list
  ___
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list


Re: [Vala] Create an instance of a derived class, from a base class method

2012-02-19 Thread Tal Hadad

It's not for you at all(and won't solve anything), but take a look at this 
example:
https://live.gnome.org/Vala/GStreamerSample

 Date: Sun, 19 Feb 2012 13:24:31 +
 Subject: Re: [Vala] Create an instance of a derived class, from a base class 
 method
 From: blowb...@gmail.com
 To: tal...@hotmail.com
 CC: vala-list@gnome.org
 
 2012/2/19 Tal Hadad tal...@hotmail.com:
 
  I don't understand what is your purpose:
  I don't understand why you don't use constructors, or static methods.
 
 I'd got myself in a bit of a mess inheriting singleton factory classes
 and static properties/methodscoming from a C background, I get a
 bit over-exciteable around OO. I've since refactored with templates
 and accepted the 'singletons are evil' lore, which has made everything
 a lot simpler.
 
  See, the type deceleration must be explicit at complication time.
  However, if you don't want it, you can always use generics and the 
  dynamic word.
 
 Meh, I very much like the concept of 'complication time'. What is this
 'dynamic' keyword of which you speak?
 
 cheers
 ant
  ___
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list