[Vala] using named constructors with runtime type determination



Hello,

I have an abstract class, which has a named constructor and would like
to be able to create instances of concrete subclasses by passing the
type to a factory method. Specifically:

abstract class Document : Object {
  Document.with_name (string name) { ... }
}

class TextDocument : Document {
  TextDocument.with_name (string name) { ... }
}

Document get_document_with_name (string name, Type document_type) {
  // Something here ...
}

// Usage
TextDocument text_document = (TextDocument) get_document_with_name
(name, typeof (TextDocument));


I have a feeling such a thing is possible using GObject constructor
properties and the construct block, but I'm not sure how. I've been
avoiding doing object construction the GObject way up until now, and
would prefer to keep doing so if possible as it is simpler to
understand.

Also this factory method is just for a special case, i.e. the normal
way to create documents is actually using the new keyword. So if I
have to use GObject construction, I would prefer it to be alongside
the normal constructors; is this possible? The documentation I've read
seems to assume it's one or the other.

Regards,
James



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