digitalmars.D - Import in functions
- Vathix (20/20) Jun 20 2005 It would be nice if we could import in functions, especially when
- Unknown W. Brackets (10/39) Jun 20 2005 I don't like it in functions, myself, because it just makes things
- Derek Parnell (22/25) Jun 20 2005 Yes. assert() tells you what the result is *not*, and not what the resul...
It would be nice if we could import in functions, especially when inserting debug code. void foo(int val) { import std.stdio; writefln("DEBUG: val = %d", val); } One particularly nice use for this is when using unittests. Often times you want to import something just for a unittest but don't want it imported otherwise. Currently, we use version() or debug() to do that. It would be a whole lot simpler to just import right in the unittest. Possible issues: for(import std.string;;) { // ... } for(;; import std.string, i++) { // ... }
Jun 20 2005
I don't like it in functions, myself, because it just makes things harder to read - although, I suppose, that's not the language's choice to enforce. Still, it wouldn't work in C either. I do very much like it in unittests, though, myself. Many times, you'll want writef for the unittest (e.g. to explain *what* failed, better than assert can) but you won't need std.stdio for the general (no unittest) case. That said, it's not like it's linked in anyway (or I should hope not!), so it's not that huge of a deal. I guess the module constructors/etc. would be called, though? -[Unknown]It would be nice if we could import in functions, especially when inserting debug code. void foo(int val) { import std.stdio; writefln("DEBUG: val = %d", val); } One particularly nice use for this is when using unittests. Often times you want to import something just for a unittest but don't want it imported otherwise. Currently, we use version() or debug() to do that. It would be a whole lot simpler to just import right in the unittest. Possible issues: for(import std.string;;) { // ... } for(;; import std.string, i++) { // ... }
Jun 20 2005
On Mon, 20 Jun 2005 03:22:46 -0700, Unknown W. Brackets wrote:I do very much like it in unittests, though, myself. Many times, you'll want writef for the unittest (e.g. to explain *what* failed, better than assert can) but you won't need std.stdio for the general (no unittest) case.Yes. assert() tells you what the result is *not*, and not what the result is. I get around this limitation by using the debug statement. <code> debug private import std.stdio; int doubleit(int x) { return 2 + x; } unittest { debug writefln("%d", doubleit(1)); assert( doubleit(1) == 2); } void main() { } </code> I know its not perfect but it's not too much imposition for me. -- Derek Parnell Melbourne, Australia 20/06/2005 9:26:45 PM
Jun 20 2005