www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to include??

reply KAR <akmalxxx gmail.com> writes:
Hello all, im a c programmer for years, and recently and recently trying to
switch for better language since my projects becoming biger and bigger, now i
need faster language in terms of development but i never fan of c++, D looks
promising but starting up can be frustating since not much in tutorial provided.

Can somebody tell me of source code filing structure, since ive been use
#include and stuff for long, and not familiar with d module and import...
Oct 17 2007
parent reply BCS <ao pathlink.com> writes:
Reply to KAR,

 Hello all, im a c programmer for years, and recently and recently
 trying to switch for better language since my projects becoming biger
 and bigger, now i need faster language in terms of development but i
 never fan of c++, D looks promising but starting up can be frustating
 since not much in tutorial provided.
 
 Can somebody tell me of source code filing structure, since ive been
 use #include and stuff for long, and not familiar with d module and
 import...
 
import fulfills the same purpose as #include. But rather than have a .h file that defines thing and a .c file that implements them, you only have one file that both defines and implements things. The import statement does it all for you. When the compiler finds an import, it does some of the compiling of the imported file, enough that it can get the list of function, classes, structs, enums etc. that are defined. These are added to the list of available symbols much like prototypes in a .h file do. Hopefully that will answer your question. If not, ask some more.
Oct 17 2007
parent reply KAR <akmalxxx gmail.com> writes:
thanks for your quick reply.

its a bit clear for me now as i found tutorial on module usage in dsource,
however does all import should be declare as a module?

btw, fyi im going to experiment D for my search engine (indexer, crawler and
search daemon), which if successful will be integrate into our projects and
might be free or opensource as well (a bit like lucene but focusing on speeed).
Oct 17 2007
parent reply BCS <ao pathlink.com> writes:
Reply to KAR,

 however does all import should be declare as a module?
 
I don't understand the question. could you please rephrase it. I'll take a few guesses though: import is only allowed at module scope, not in a class or function or sutch. only a module can be imported, you can't import all modules in a directory (a apckage) with one import, you need to import each module by its self. You can import only a few symbols from a module with selective imports (that's in the docs).
Oct 17 2007
next sibling parent KAR <akmalxxx gmail.com> writes:
BCS Wrote:

 Reply to KAR,
 
 however does all import should be declare as a module?
 
I don't understand the question. could you please rephrase it. I'll take a few guesses though: import is only allowed at module scope, not in a class or function or sutch. only a module can be imported, you can't import all modules in a directory (a apckage) with one import, you need to import each module by its self. You can import only a few symbols from a module with selective imports (that's in the docs).
im getting the ideas now. thanks alot
Oct 17 2007
prev sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
BCS wrote:
 import is only allowed at module scope, not in a class or function or 
 sutch.
IIRC import is allowed in classes. Also in structs, version, debug, etc.
Oct 18 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Ary,

 BCS wrote:
 
 import is only allowed at module scope, not in a class or function or
 sutch.
 
IIRC import is allowed in classes.
Oh. it is? Didn't know that
 Also in structs, version, debug, etc.
 
well the inside of static if, version and debug are technically at the same scope as the outside so...
Oct 18 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
BCS wrote:
 Reply to Ary,
 
 BCS wrote:

 import is only allowed at module scope, not in a class or function or
 sutch.
IIRC import is allowed in classes.
Oh. it is? Didn't know that
It is "allowed" there (no compiler error) but it has no effect there. It only has effect at module scope. -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 18 2007
parent reply Ary Manzana <ary esperanto.org.ar> writes:
This compiles, runs, and outputs "hola" as expected:

---
class X {
	import std.stdio;
	void foo() {
		writefln("hola");
	}
}

void main() {
	(new X()).foo();
}
---

Even more, if you move the foo function outside X, it doesn't compile 
because it can't find "writefln". If I remember correctly from what I've 
seen in DMD's code, the import loads the imported symbols into the 
symbol table of the current scope (X, in this case). So... it works. :-)

Bruno Medeiros wrote:
 BCS wrote:
 Reply to Ary,

 BCS wrote:

 import is only allowed at module scope, not in a class or function or
 sutch.
IIRC import is allowed in classes.
Oh. it is? Didn't know that
It is "allowed" there (no compiler error) but it has no effect there. It only has effect at module scope.
Oct 18 2007
next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Ary Manzana wrote:
 This compiles, runs, and outputs "hola" as expected:
 
 ---
 class X {
     import std.stdio;
     void foo() {
         writefln("hola");
     }
 }
 
 void main() {
     (new X()).foo();
 }
 ---
 
 Even more, if you move the foo function outside X, it doesn't compile 
 because it can't find "writefln". If I remember correctly from what I've 
 seen in DMD's code, the import loads the imported symbols into the 
 symbol table of the current scope (X, in this case). So... it works. :-)
Hey, then maybe that's a good workaround for no imports in unittests. Just make a big Test class inside unittest{...} that does all the work. --bb
Oct 18 2007
parent reply Witold Baryluk <baryluk smp.if.uj.edu.pl> writes:
Dnia Fri, 19 Oct 2007 10:45:27 +0900
Bill Baxter <dnewsgroup billbaxter.com> napisa=B3/a:

 Hey, then maybe that's a good workaround for no imports in unittests.
 Just make a big Test class inside unittest{...} that does all the
 work.
=20
 --bb
I also didn't know that import are possible in classes. unittest { class Test { import std.stdio static test() { ... } } Test.test(); } hackish :) Imports in functions and unittest will be also be helpful. --=20 Witold Baryluk, aleph0
Oct 18 2007
parent reply Ary Manzana <ary esperanto.org.ar> writes:
The problem is that in a module, class or struct scope (and the rest 
where imports are now allowed) you can just put any declaration you 
want. This is not the case in a function. For example, you can't declare 
a template inside a function.

Humm... And I'm out of examples. Are templates the only declarations not 
allwed in a function? Because otherwise, I think it would be possible to 
import inside a function or unittest...

Witold Baryluk escribió:
 Dnia Fri, 19 Oct 2007 10:45:27 +0900
 Bill Baxter <dnewsgroup billbaxter.com> napisa³/a:
 
 Hey, then maybe that's a good workaround for no imports in unittests.
 Just make a big Test class inside unittest{...} that does all the
 work.

 --bb
I also didn't know that import are possible in classes. unittest { class Test { import std.stdio static test() { ... } } Test.test(); } hackish :) Imports in functions and unittest will be also be helpful.
Oct 18 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Ary Manzana wrote:
 The problem is that in a module, class or struct scope (and the rest 
 where imports are now allowed) you can just put any declaration you 
 want. This is not the case in a function. For example, you can't declare 
 a template inside a function.
 
 Humm... And I'm out of examples. Are templates the only declarations not 
 allwed in a function? Because otherwise, I think it would be possible to 
 import inside a function or unittest...
I just noticed the other day that you can't declare overloaded functions inside a function. eg: void foo() { void blarf(int x) { } void blarf(string x) {} blarf(3); blarf("hi there"); } --bb
Oct 18 2007
prev sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Ary Manzana wrote:
 This compiles, runs, and outputs "hola" as expected:
 
 ---
 class X {
     import std.stdio;
     void foo() {
         writefln("hola");
     }
 }
 
 void main() {
     (new X()).foo();
 }
 ---
 
 Even more, if you move the foo function outside X, it doesn't compile 
 because it can't find "writefln". If I remember correctly from what I've 
 seen in DMD's code, the import loads the imported symbols into the 
 symbol table of the current scope (X, in this case). So... it works. :-)
 
Hum, you're right. That's quite odd, I has the distinct impression of trying that before (when looking how Mmrnmhrm functionality should work) and it not working that way. Was there maybe some DMD version that didn't work that way I wonder?... -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 19 2007
prev sibling parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Ary Manzana wrote:
 BCS wrote:
 import is only allowed at module scope, not in a class or function or 
 sutch.
IIRC import is allowed in classes. Also in structs, version, debug, etc.
Apparently it's allowed in function/statements scope too, with a workaround: template Tpl() { import std.stdio; } void func() { mixin Tpl!(); writefln("Foo"); } void main() { //writefln("Foo"); // writefln not available func(); } Seems it's only disallowed on a syntactic level. -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 20 2007
parent Ary Manzana <ary esperanto.org.ar> writes:
Bruno Medeiros escribió:
 Ary Manzana wrote:
 BCS wrote:
 import is only allowed at module scope, not in a class or function or 
 sutch.
IIRC import is allowed in classes. Also in structs, version, debug, etc.
Apparently it's allowed in function/statements scope too, with a workaround: template Tpl() { import std.stdio; } void func() { mixin Tpl!(); writefln("Foo"); } void main() { //writefln("Foo"); // writefln not available func(); } Seems it's only disallowed on a syntactic level.
Smart :-)
Oct 20 2007