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



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 ();
        }
}

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



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