Re: [Vala] Implementing Singleton
- From: Jan Hudec <bulb ucw cz>
- To: Cem Eliguzel <celiguzel ubicom com>
- Cc: vala-list gnome org
- Subject: Re: [Vala] Implementing Singleton
- Date: Mon, 4 Jan 2010 14:31:12 +0100
On Mon, Jan 04, 2010 at 15:02:13 +0200, Cem Eliguzel wrote:
Hello,
I have the following simple singleton implementation:
public class Singleton<T> : Object {
private static T instance_;
public static T instance() {
if (instance_ == null) {
instance_ = new T();
}
return instance_;
}
}
and I get the following error during compilation:
error: `Singleton.T' is not a class, struct, or error code
Wat is the point I'm missing?
The generics in Vala only work like they do in Java -- they are compiled just
once, for the most generic type (which is a void*). That also means static
members only exists once. Also static methods don't have access to the type
argument.
I am not sure class methods and fields would be specific to the
specialization, but I don't think so either. What you could have is a generic
"instance" method, that would store the instance in a hash table keyed on the
type.
However, the construction seems to me like it's missing the point of
singleton. That is that the class itself needs to ensure it only exists once.
So each class would have the static member, getter for it and private
constructor.
--
Jan 'Bulb' Hudec <bulb ucw cz>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]