October 19, 2007
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.

-- 
Witold Baryluk, aleph0
October 19, 2007
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.
> 
October 19, 2007
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
October 19, 2007
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
October 20, 2007
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
October 20, 2007
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 :-)
1 2
Next ›   Last »