Why import statements are not allowed in function bodies (or any block of code)?

Russell Lewis webmaster at villagersonline.com
Sun Jan 27 19:37:49 PST 2008


I often add new imports for some temporary debugging code (such as 
importing std.gc so that I can call fullCollect() to debug a memory 
leak) but then forget to remove them later.

I wonder: if DMD can produce the list of imports, then is there any need 
for (mandatory) imports anymore?  Why not just let a fully-qualified 
name automatically import the right module?  An import statement, then, 
would just be a way of importing an entire module's set of symbols so 
that you didn't have to fully-qualify everything for heavily used modules.

Bill Baxter wrote:
> Leandro Lucarella wrote:
>> Is there any good reason for not allowing import statements in function
>> bodies or any code block? I think it would be really useful for avoid
>> namespace polution, specially in unittests (it's really ugly to import
>> things a module will never use except for unittesting).
>>
> 
> Did someone already suggest using the version(UnitTest) workaround?
> 
> I do remember hearing some reason before, like it makes it easier for 
> tools to scrape out all the imports from a file.  But seems like a lame 
> excuse to me now that DMD can spit out a list of imports itself.  I 
> would guess it's just a matter of not having a pressing enough need for 
> it.  But it would be nice.
> 
> --bb



More information about the Digitalmars-d mailing list