View mode: basic / threaded / horizontal-split · Log in · Help
May 11, 2004
Package Import
In D, is it possible to import an entire package?  Better yet, is it
possible to break up a module into several files?  I rather like Java's
method of packaging, however D's method seems somewhat cumbersome, since it
appears to me that all of a module's classes must be in a single file.  I
don't trust my version control system's merging algorithm that much.

Or am I completely missing something, as a D newbie?

Thanks in advance
Demme
May 11, 2004
Re: Package Import
DemmeGod wrote:

>In D, is it possible to import an entire package?  Better yet, is it
>possible to break up a module into several files?  I rather like Java's
>method of packaging, however D's method seems somewhat cumbersome, since it
>appears to me that all of a module's classes must be in a single file.  I
>don't trust my version control system's merging algorithm that much.
>
>Or am I completely missing something, as a D newbie?
>
>Thanks in advance
>Demme
>  
>
What I would do to break things is put all the classes you need to be 
friends (normally quite rare), in the same module.  Everything else can 
can go in other modules and you can link them together using import.

-- 
-Anderson: http://badmama.com.au/~anderson/
May 11, 2004
Re: Package Import
A module is more or less a file.
You can have one module foreach class if you want.

Take a look at this URL:

http://www.digitalmars.com/d/module.html

Phill.

"DemmeGod" <me@demmegod.com> wrote in message
news:c7pnss$28ea$1@digitaldaemon.com...
> In D, is it possible to import an entire package?  Better yet, is it
> possible to break up a module into several files?  I rather like Java's
> method of packaging, however D's method seems somewhat cumbersome, since
it
> appears to me that all of a module's classes must be in a single file.  I
> don't trust my version control system's merging algorithm that much.
>
> Or am I completely missing something, as a D newbie?
May 11, 2004
Re: Package Import
I've read the specs...  I understand that, however is one does it that way,
one must import each class that one indends on using.  My question was if
it was possible to import an entire class, that is import a whole bunch on
modules.  I take it that this is not possible?

Phill wrote:

> A module is more or less a file.
> You can have one module foreach class if you want.
> 
> Take a look at this URL:
> 
> http://www.digitalmars.com/d/module.html
> 
> Phill.
> 
> "DemmeGod" <me@demmegod.com> wrote in message
> news:c7pnss$28ea$1@digitaldaemon.com...
>> In D, is it possible to import an entire package?  Better yet, is it
>> possible to break up a module into several files?  I rather like Java's
>> method of packaging, however D's method seems somewhat cumbersome, since
> it
>> appears to me that all of a module's classes must be in a single file.  I
>> don't trust my version control system's merging algorithm that much.
>>
>> Or am I completely missing something, as a D newbie?
May 11, 2004
Re: Package Import
J Anderson wrote:

> DemmeGod wrote:
> 
>>In D, is it possible to import an entire package?  Better yet, is it
>>possible to break up a module into several files?  I rather like Java's
>>method of packaging, however D's method seems somewhat cumbersome, since
>>it
>>appears to me that all of a module's classes must be in a single file.  I
>>don't trust my version control system's merging algorithm that much.
>>
>>Or am I completely missing something, as a D newbie?
>>
>>Thanks in advance
>>Demme
>>  
>>
> What I would do to break things is put all the classes you need to be
> friends (normally quite rare), in the same module.  Everything else can
> can go in other modules and you can link them together using import.
> 

When you say link them together, how do you mean?  Do you mean use a ton of
imports in each file that is using them?  Or is it possible to import
modules into another module, which will in turn be imported when one wishes
to use all of the modules?

Thanks
Demme
May 11, 2004
Re: Package Import
On Tue, 11 May 2004 13:23:59 -0400, DemmeGod <me@demmegod.com> wrote:
> I've read the specs...  I understand that, however is one does it that 
> way, one must import each class that one indends on using.  My question 
> was if
> it was possible to import an entire class, that is import a whole bunch 
> on modules.  I take it that this is not possible?

You mean something like this...

import module1;

[test.d]
import std.string;
import module1;

int main () {
	m1(5);
	m2(8);
	m3(9);
	return 0;
}

[module1.d]
module module1;

import module2;
import module3;

void m1(int a) {
	printf("module1: %d\n",a);
	m2(++a);
	m3(++a);
}

[module2.d]
module module2;

void m2(int a) {
	printf("module2: %d\n",a);
}

[module3.d]
module module3;

void m3(int a) {
	printf("module3: %d\n",a);
}


i.e. importing module1 implicitly imports modules 2 and 3.

Regan.

> Phill wrote:
>
>> A module is more or less a file.
>> You can have one module foreach class if you want.
>>
>> Take a look at this URL:
>>
>> http://www.digitalmars.com/d/module.html
>>
>> Phill.
>>
>> "DemmeGod" <me@demmegod.com> wrote in message
>> news:c7pnss$28ea$1@digitaldaemon.com...
>>> In D, is it possible to import an entire package?  Better yet, is it
>>> possible to break up a module into several files?  I rather like Java's
>>> method of packaging, however D's method seems somewhat cumbersome, 
>>> since
>> it
>>> appears to me that all of a module's classes must be in a single 
>>> file.  I
>>> don't trust my version control system's merging algorithm that much.
>>>
>>> Or am I completely missing something, as a D newbie?
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 11, 2004
Re: Package Import
"DemmeGod" <me@demmegod.com> wrote in message
news:c7r27f$15qf$1@digitaldaemon.com...
> I've read the specs...  I understand that, however is one does it that
way,
> one must import each class that one indends on using.  My question was if
> it was possible to import an entire class, that is import a whole bunch on
> modules.  I take it that this is not possible?

If you mean you want to import "a whole bunch of modules" then Regans way is
the way to go.
May 12, 2004
Re: Package Import
That's precisely what I mean... I get a compile error with something similar
to that, however.
I figure I'd write a lightweight logger:

[com/demmegod/lwLog4D.d]
module com.demmegod.lwLog4D;

import com.demmegod.lwLog4D.Logger;

[com/demmegod/lwLog4D/Logger.d]
module com.demmegod.lwLog4D.Logger;

import com.demmegod.lwLog4D;

class Logger
{
       Appender[] appenders = [];      
       static Logger globalLogger;
       
       static Logger getGlobalLogger()
       {
               ...
       }
       
       static void setGlobalLogger(Logger _globalLogger)
       {
               ...
       }
       
       this()
       {
               ...
       }
       
       void log(LogPriority priority, char[] message)
       {
               ...
       }
       
       void addAppender(Appender appender)
       {
               ...
       }
       
       void removeAppender(Appender appender)
       {
               ...
       }
}

...Plus more classes...

When I try to compile lwLog4D.d, I get the following error:
com/demmegod/lwLog4D.d(1): module lwLog4D module and package have the same
name

I assumed that meant that I couldn't import com.demmegod.lwLog4D.Logger. 
I'd like to import com.demmegod.lwLog4D and have access to multiple classes
in separate files (Logger being one example.)

Yes, I know I'm violating convention by using upper case characters in the
module and package names... I'll change that.

Thanks

Regan Heath wrote:

> On Tue, 11 May 2004 13:23:59 -0400, DemmeGod <me@demmegod.com> wrote:
>> I've read the specs...  I understand that, however is one does it that
>> way, one must import each class that one indends on using.  My question
>> was if
>> it was possible to import an entire class, that is import a whole bunch
>> on modules.  I take it that this is not possible?
> 
> You mean something like this...
> 
> import module1;
> 
> [test.d]
> import std.string;
> import module1;
> 
> int main () {
> m1(5);
> m2(8);
> m3(9);
> return 0;
> }
> 
> [module1.d]
> module module1;
> 
> import module2;
> import module3;
> 
> void m1(int a) {
> printf("module1: %d\n",a);
> m2(++a);
> m3(++a);
> }
> 
> [module2.d]
> module module2;
> 
> void m2(int a) {
> printf("module2: %d\n",a);
> }
> 
> [module3.d]
> module module3;
> 
> void m3(int a) {
> printf("module3: %d\n",a);
> }
> 
> 
> i.e. importing module1 implicitly imports modules 2 and 3.
> 
> Regan.
> 
>> Phill wrote:
>>
>>> A module is more or less a file.
>>> You can have one module foreach class if you want.
>>>
>>> Take a look at this URL:
>>>
>>> http://www.digitalmars.com/d/module.html
>>>
>>> Phill.
>>>
>>> "DemmeGod" <me@demmegod.com> wrote in message
>>> news:c7pnss$28ea$1@digitaldaemon.com...
>>>> In D, is it possible to import an entire package?  Better yet, is it
>>>> possible to break up a module into several files?  I rather like Java's
>>>> method of packaging, however D's method seems somewhat cumbersome,
>>>> since
>>> it
>>>> appears to me that all of a module's classes must be in a single
>>>> file.  I
>>>> don't trust my version control system's merging algorithm that much.
>>>>
>>>> Or am I completely missing something, as a D newbie?
>>
> 
> 
>
May 12, 2004
Re: Package Import
What are you writing it in? I am using DIDE which works quite well, you 
can get it here:
  http://www.atari-soldiers.com/dide.html

Here is a cut down code sample that compiles and runs, it does what you 
are trying to do (I think):

[test.d]
import std.string;
import com.demmegod.module2;

int main () {
	Module2 m = new Module2();
	m.m(0);
	return 0;
}

[/com/demmegod/module2.d]
module com.demmegod.module2;
import com.demmegod.module3;

class Module2 {
	void m(int a) {
		printf("module2: %d\n",a);
		if ( a < 100 ) {
			Module3 m = new Module3();
			m.m(++a);
		}
	}
}

[/com/demmegod/module3.d]
module com.demmegod.module3;
import com.demmegod.module2;

class Module3 {
	void m(int a) {
		printf("module3: %d\n",a);
		if ( a < 100 ) {
			Module2 m = new Module2();
			m.m(++a);
		}
	}
}

Regan.

On Tue, 11 May 2004 20:04:32 -0400, DemmeGod <me@demmegod.com> wrote:
> That's precisely what I mean... I get a compile error with something 
> similar to that, however.
> I figure I'd write a lightweight logger:
>
> [com/demmegod/lwLog4D.d]
> module com.demmegod.lwLog4D;
>
> import com.demmegod.lwLog4D.Logger;
>
> [com/demmegod/lwLog4D/Logger.d]
> module com.demmegod.lwLog4D.Logger;
>
> import com.demmegod.lwLog4D;
>
> class Logger
> {
>         Appender[] appenders = [];
>         static Logger globalLogger;
>
>         static Logger getGlobalLogger()
>         {
>                 ...
>         }
>
>         static void setGlobalLogger(Logger _globalLogger)
>         {
>                 ...
>         }
>
>         this()
>         {
>                 ...
>         }
>
>         void log(LogPriority priority, char[] message)
>         {
>                 ...
>         }
>
>         void addAppender(Appender appender)
>         {
>                 ...
>         }
>
>         void removeAppender(Appender appender)
>         {
>                 ...
>         }
> }
>
> ...Plus more classes...
>
> When I try to compile lwLog4D.d, I get the following error:
> com/demmegod/lwLog4D.d(1): module lwLog4D module and package have the 
> same
> name
>
> I assumed that meant that I couldn't import com.demmegod.lwLog4D.Logger.
> I'd like to import com.demmegod.lwLog4D and have access to multiple 
> classes
> in separate files (Logger being one example.)
>
> Yes, I know I'm violating convention by using upper case characters in 
> the
> module and package names... I'll change that.
>
> Thanks
>
> Regan Heath wrote:
>
>> On Tue, 11 May 2004 13:23:59 -0400, DemmeGod <me@demmegod.com> wrote:
>>> I've read the specs...  I understand that, however is one does it that
>>> way, one must import each class that one indends on using.  My question
>>> was if
>>> it was possible to import an entire class, that is import a whole bunch
>>> on modules.  I take it that this is not possible?
>>
>> You mean something like this...
>>
>> import module1;
>>
>> [test.d]
>> import std.string;
>> import module1;
>>
>> int main () {
>> m1(5);
>> m2(8);
>> m3(9);
>> return 0;
>> }
>>
>> [module1.d]
>> module module1;
>>
>> import module2;
>> import module3;
>>
>> void m1(int a) {
>> printf("module1: %d\n",a);
>> m2(++a);
>> m3(++a);
>> }
>>
>> [module2.d]
>> module module2;
>>
>> void m2(int a) {
>> printf("module2: %d\n",a);
>> }
>>
>> [module3.d]
>> module module3;
>>
>> void m3(int a) {
>> printf("module3: %d\n",a);
>> }
>>
>>
>> i.e. importing module1 implicitly imports modules 2 and 3.
>>
>> Regan.
>>
>>> Phill wrote:
>>>
>>>> A module is more or less a file.
>>>> You can have one module foreach class if you want.
>>>>
>>>> Take a look at this URL:
>>>>
>>>> http://www.digitalmars.com/d/module.html
>>>>
>>>> Phill.
>>>>
>>>> "DemmeGod" <me@demmegod.com> wrote in message
>>>> news:c7pnss$28ea$1@digitaldaemon.com...
>>>>> In D, is it possible to import an entire package?  Better yet, is it
>>>>> possible to break up a module into several files?  I rather like 
>>>>> Java's
>>>>> method of packaging, however D's method seems somewhat cumbersome,
>>>>> since
>>>> it
>>>>> appears to me that all of a module's classes must be in a single
>>>>> file.  I
>>>>> don't trust my version control system's merging algorithm that much.
>>>>>
>>>>> Or am I completely missing something, as a D newbie?
>>>
>>
>>
>>
>



-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
May 12, 2004
Re: Package Import
Thats not quite accurate... You'll get a compile error if you move module3
to com.demmegod.module2.module3.

DIDE looks nice, but it also looks like it's Windows only.  I'm Linux and
OSX only.

Thanks

Regan Heath wrote:

> What are you writing it in? I am using DIDE which works quite well, you
> can get it here:
>    http://www.atari-soldiers.com/dide.html
> 
> Here is a cut down code sample that compiles and runs, it does what you
> are trying to do (I think):
> 
> [test.d]
> import std.string;
> import com.demmegod.module2;
> 
> int main () {
> Module2 m = new Module2();
> m.m(0);
> return 0;
> }
> 
> [/com/demmegod/module2.d]
> module com.demmegod.module2;
> import com.demmegod.module3;
> 
> class Module2 {
> void m(int a) {
> printf("module2: %d\n",a);
> if ( a < 100 ) {
> Module3 m = new Module3();
> m.m(++a);
> }
> }
> }
> 
> [/com/demmegod/module3.d]
> module com.demmegod.module3;
> import com.demmegod.module2;
> 
> class Module3 {
> void m(int a) {
> printf("module3: %d\n",a);
> if ( a < 100 ) {
> Module2 m = new Module2();
> m.m(++a);
> }
> }
> }
> 
> Regan.
> 
> On Tue, 11 May 2004 20:04:32 -0400, DemmeGod <me@demmegod.com> wrote:
>> That's precisely what I mean... I get a compile error with something
>> similar to that, however.
>> I figure I'd write a lightweight logger:
>>
>> [com/demmegod/lwLog4D.d]
>> module com.demmegod.lwLog4D;
>>
>> import com.demmegod.lwLog4D.Logger;
>>
>> [com/demmegod/lwLog4D/Logger.d]
>> module com.demmegod.lwLog4D.Logger;
>>
>> import com.demmegod.lwLog4D;
>>
>> class Logger
>> {
>>         Appender[] appenders = [];
>>         static Logger globalLogger;
>>
>>         static Logger getGlobalLogger()
>>         {
>>                 ...
>>         }
>>
>>         static void setGlobalLogger(Logger _globalLogger)
>>         {
>>                 ...
>>         }
>>
>>         this()
>>         {
>>                 ...
>>         }
>>
>>         void log(LogPriority priority, char[] message)
>>         {
>>                 ...
>>         }
>>
>>         void addAppender(Appender appender)
>>         {
>>                 ...
>>         }
>>
>>         void removeAppender(Appender appender)
>>         {
>>                 ...
>>         }
>> }
>>
>> ...Plus more classes...
>>
>> When I try to compile lwLog4D.d, I get the following error:
>> com/demmegod/lwLog4D.d(1): module lwLog4D module and package have the
>> same
>> name
>>
>> I assumed that meant that I couldn't import com.demmegod.lwLog4D.Logger.
>> I'd like to import com.demmegod.lwLog4D and have access to multiple
>> classes
>> in separate files (Logger being one example.)
>>
>> Yes, I know I'm violating convention by using upper case characters in
>> the
>> module and package names... I'll change that.
>>
>> Thanks
>>
>> Regan Heath wrote:
>>
>>> On Tue, 11 May 2004 13:23:59 -0400, DemmeGod <me@demmegod.com> wrote:
>>>> I've read the specs...  I understand that, however is one does it that
>>>> way, one must import each class that one indends on using.  My question
>>>> was if
>>>> it was possible to import an entire class, that is import a whole bunch
>>>> on modules.  I take it that this is not possible?
>>>
>>> You mean something like this...
>>>
>>> import module1;
>>>
>>> [test.d]
>>> import std.string;
>>> import module1;
>>>
>>> int main () {
>>> m1(5);
>>> m2(8);
>>> m3(9);
>>> return 0;
>>> }
>>>
>>> [module1.d]
>>> module module1;
>>>
>>> import module2;
>>> import module3;
>>>
>>> void m1(int a) {
>>> printf("module1: %d\n",a);
>>> m2(++a);
>>> m3(++a);
>>> }
>>>
>>> [module2.d]
>>> module module2;
>>>
>>> void m2(int a) {
>>> printf("module2: %d\n",a);
>>> }
>>>
>>> [module3.d]
>>> module module3;
>>>
>>> void m3(int a) {
>>> printf("module3: %d\n",a);
>>> }
>>>
>>>
>>> i.e. importing module1 implicitly imports modules 2 and 3.
>>>
>>> Regan.
>>>
>>>> Phill wrote:
>>>>
>>>>> A module is more or less a file.
>>>>> You can have one module foreach class if you want.
>>>>>
>>>>> Take a look at this URL:
>>>>>
>>>>> http://www.digitalmars.com/d/module.html
>>>>>
>>>>> Phill.
>>>>>
>>>>> "DemmeGod" <me@demmegod.com> wrote in message
>>>>> news:c7pnss$28ea$1@digitaldaemon.com...
>>>>>> In D, is it possible to import an entire package?  Better yet, is it
>>>>>> possible to break up a module into several files?  I rather like
>>>>>> Java's
>>>>>> method of packaging, however D's method seems somewhat cumbersome,
>>>>>> since
>>>>> it
>>>>>> appears to me that all of a module's classes must be in a single
>>>>>> file.  I
>>>>>> don't trust my version control system's merging algorithm that much.
>>>>>>
>>>>>> Or am I completely missing something, as a D newbie?
>>>>
>>>
>>>
>>>
>>
> 
> 
>
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home