Re: [Vala] Generics: constraints and 'new T ()' on type parameters?



Hi Sam,

On Thu, 2009-02-26 at 19:52 -0500, Sam Danielson wrote:
I'm writing some ORM where a database view is wrapped in a factory
object that produces records. Ideally I would think a Factory <T>
should be able to return new T's but I can't figure out how to do
this. The work around is to return the Record base class and then
upcast, or put virtual upcasting functions in all of the
derived Factory classes. Yuck.

Is there a fundamental reason why this cant work?

class Foo {}
class Factory <T> {
      public static T make () {
              return new T ();
      }
}


Suppose Foo is the root of all the products from your factory, I suggest
using this instead of those ugly generics.

class Foo:Object {
   
}

class Foo1:Foo {

}
class Factory {
    public static Foo make(Type type) {
          return Object.new (type) as Foo;
    }
}
The types can be obtained with typeof(Foo1). The constraints can be done
at run-time with Type.is_a() or =.

BTW: shouldn't a factory has one method for making each type of objects?

Yu

And is there a plan to implement type parameter constraints? Something like...

class Factory <T:Object> { ... }

The compiler may then prove that it is okay to 'new T ()'

PS. I can slip the following by the type system without a complaint. Should I
file a bug?

class Foo {}
class Factory <T> {
      public static T make () {
              return new Foo ();
      }
}

LLAP,
Sam Danielson
_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




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