www.digitalmars.com         C & C++   DMDScript  

D - Singleton

reply Evain Jb <Evain_member pathlink.com> writes:
This snippet compiles well :

import std.c.stdio;

class Singleton {
private:
static this() {
m_inst = null;
}
static Singleton m_inst = null;

char[] m_sentence;

this() {
m_sentence = "Hello Singleton";
}
public:
static Singleton inst() {
if (m_inst == null)  {
m_inst = new Singleton();
} 
return m_inst;
}

char[] sentence() {
return m_sentence;
}
}

int main(char[][] args) {
assert(Singleton.inst != null);
puts(Singleton.inst.sentence);
return 1;
}

But when I try to execute it, here is the output :

D:\software\dide\Projects\Proto>proto
Error: Access Violation

And using 

Digital Mars D Compiler v0.86

Do I miss something ?
May 04 2004
next sibling parent reply Stephan Wienczny <wienczny web.de> writes:
You should create your Singleton before using it.

Evain Jb wrote:
 This snippet compiles well :
 
 import std.c.stdio;
 
 class Singleton {
 private:
 static this() {
 m_inst = null;
 }
 static Singleton m_inst = null;
 
 char[] m_sentence;
 
 this() {
 m_sentence = "Hello Singleton";
 }
 public:
 static Singleton inst() {
 if (m_inst == null)  {
 m_inst = new Singleton();
 } 
 return m_inst;
 }
 
 char[] sentence() {
 return m_sentence;
 }
 }
 
 int main(char[][] args) {
 assert(Singleton.inst != null);
 puts(Singleton.inst.sentence);
 return 1;
 }
 
 But when I try to execute it, here is the output :
 
 D:\software\dide\Projects\Proto>proto
 Error: Access Violation
 
 And using 
 
 Digital Mars D Compiler v0.86
 
 Do I miss something ?
 
 
 
 
 

May 04 2004
parent Evain Jb <Evain_member pathlink.com> writes:
In article <c78ocd$109d$1 digitaldaemon.com>, Stephan Wienczny says...

You should create your Singleton before using it.

The primary goal of the Singleton is not to have to create it before using it. The field m_inst, the instance of the Singleton, is created at the first access of the static accessor inst() By the way, to prevent the Singleton to be created, the constructor is private. That is why I'm wondering on the position of the access violation.
May 04 2004
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
 public:
 static Singleton inst() {
 if (m_inst == null)  {
 m_inst = new Singleton();
 }
 return m_inst;
 }

I think the == should be "is" or "==="
May 04 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
Yeah; that syntax seems to be a common issue, and it takes a little while
for it to become second nature.

Use (x is null) or (x === null)

Conversely, use (x !== null) or !(x is null) or just (x)

I suppose it might be nice to have a corresponding "not" keyword (to match
"is") but ...

Also, when testing arrays handles the codegen is better if you are explicit
about the null test, rather than doing the simple (x) test. The reason being
that array handles are 64bits wide rather than 32 (includes the length).
FWIW, I've taken to testing the length of an array since that's valid
whether the pointer is or not. To me it just looks a little cleaner ...

- Kris

"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:c78si2$175t$1 digitaldaemon.com...
 public:
 static Singleton inst() {
 if (m_inst == null)  {
 m_inst = new Singleton();
 }
 return m_inst;
 }

I think the == should be "is" or "==="

May 04 2004
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
(I'm mostly cutting and pasting from the last time I wrote this.)

Just so you know...

This is the "old" newsgroup. Please post new threads in the new newsgroup.


*GENERAL D Discussions*
Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D
Newsgroup: news://news.digitalmars.com/digitalmars.D

*BUG REPORTS for DMD*
Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs
Newsgroup: news://news.digitalmars.com/digitalmars.D.bugs

*D GNU newsgroup*
Web: http://www.digitalmars.com/drn-bin/wwwnews?D.gnu
Newsgroup: news://news.digitalmars.com/D.gnu

*Old D newsgroup* (Please don't post new threads here!)
Web: http://www.digitalmars.com/drn-bin/wwwnews?D
Newsgroup: news://news.digitalmars.com/D


Unofficial forums...

*dsource.org Forums* (phpBB)
http://www.dsource.org/forums/



It's also good courtesy to examine the Frequently Asked Questions before 
posting:

http://www.digitalmars.com/d/faq.html
http://www.wikiservice.at/d/wiki.cgi?FaqRoadmap


-- 
Justin
http://jcc_7.tripod.com/d/
May 04 2004