www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling a static method ?

reply andy.dwelly safedataco.com writes:
I'm taking a first look at D. I have a singleton class:

class EventQueue {
private static EventQueue _instance;

private this() {
}

public static EventQueue instance() {
if (_instance is null) {
_instance = new EventQueue;
}

return _instance;        
}
}

int main(char[][] args) {
auto var = EventQueue.instance();
return 0;
}

which compiles quite happiliy (WinXP, dmd 0.162) with dmd -c EventQueue.d , I
have another file Frame.d:

import Component;
import EventQueue;

class Frame: Component {
private EventQueue _queue;

private this(char[] title) {
EventQueue q = EventQueue.instance;
}


public void add(Component c) {
}
}

..which is trying to create an event queue. When I compile this with dmd -c
Frame.d I get an:

Frame.d(19): undefined identifier module EventQueue.instance

What am I doing wrong here ? The fact that the main function in the EventQueue.d
file suggests that its some kind of linker or import error, the instance()
method in EventQueue seems to be OK.

Can somebody give me some guidance please.


Andy Dwelly
Jul 13 2006
next sibling parent Brad Anderson <brad dsource.org> writes:
andy.dwelly safedataco.com wrote:
 I'm taking a first look at D. I have a singleton class:
 
 class EventQueue {
 private static EventQueue _instance;
 
 private this() {
 }
 
 public static EventQueue instance() {
 if (_instance is null) {
 _instance = new EventQueue;
 }
 
 return _instance;        
 }
 }
 
 int main(char[][] args) {
 auto var = EventQueue.instance();
 return 0;
 }
 
 which compiles quite happiliy (WinXP, dmd 0.162) with dmd -c EventQueue.d , I
 have another file Frame.d:
 
 import Component;
 import EventQueue;
 
 class Frame: Component {
 private EventQueue _queue;
 
 private this(char[] title) {
 EventQueue q = EventQueue.instance;
 }
 
 
 public void add(Component c) {
 }
 }
 
 ..which is trying to create an event queue. When I compile this with dmd -c
 Frame.d I get an:
 
 Frame.d(19): undefined identifier module EventQueue.instance
 
 What am I doing wrong here ? The fact that the main function in the
EventQueue.d
 file suggests that its some kind of linker or import error, the instance()
 method in EventQueue seems to be OK.
 
 Can somebody give me some guidance please.
 
 
 Andy Dwelly

have you tried to add: module EventQueue; to the first file? BA
Jul 13 2006
prev sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
andy.dwelly safedataco.com wrote:

 I'm taking a first look at D. I have a singleton class:
 
 class EventQueue {
 private static EventQueue _instance;
 
 private this() {
 }
 
 public static EventQueue instance() {
 if (_instance is null) {
 _instance = new EventQueue;
 }
 
 return _instance;
 }
 }
 
 int main(char[][] args) {
 auto var = EventQueue.instance();
 return 0;
 }
 
 which compiles quite happiliy (WinXP, dmd 0.162) with dmd -c EventQueue.d
 , I have another file Frame.d:
 
 import Component;
 import EventQueue;
 
 class Frame: Component {
 private EventQueue _queue;
 
 private this(char[] title) {
 EventQueue q = EventQueue.instance;
 }
 
 
 public void add(Component c) {
 }
 }
 
 ..which is trying to create an event queue. When I compile this with dmd
 -c Frame.d I get an:
 
 Frame.d(19): undefined identifier module EventQueue.instance
 
 What am I doing wrong here ? The fact that the main function in the
 EventQueue.d file suggests that its some kind of linker or import error,
 the instance() method in EventQueue seems to be OK.
 
 Can somebody give me some guidance please.
 
 
 Andy Dwelly

Hmm, the problem here is that the module is named EventQueue too. You can try to add () to your instance invocation in Frame and see if that helps, otherwise you can change the module name (and filename) to lowercase. If you want this to be in a larger project, you might want to add an extra level to your hierarchy, such that the module name becomes myproject.EventQueue. -- Lars Ivar Igesund blog at http://larsivi.net DSource & #D: larsivi
Jul 13 2006
parent reply "Andy Dwelly" <andy.dwelly safedataco.com> writes:
Ah. Modules are a topic I haven't tackled yet.

Changing the filenames to lower case and the imports as well fixes this.

So modules contain classes and functions, and import, imports modules - is 
that how it works ?

Is there any documentation covering this anywhere ? 
Jul 13 2006
parent Brad Anderson <brad dsource.org> writes:
Andy Dwelly wrote:
 Ah. Modules are a topic I haven't tackled yet.
 
 Changing the filenames to lower case and the imports as well fixes this.
 
 So modules contain classes and functions, and import, imports modules - is 
 that how it works ?
 
 Is there any documentation covering this anywhere ? 
 
 

http://www.digitalmars.com/d/module.html
Jul 13 2006