Function-local imports

retard re at tard.com.invalid
Mon Apr 26 16:21:38 PDT 2010


Mon, 26 Apr 2010 04:40:09 +0000, Jesse Phillips wrote:

> bearophile wrote:
> 
>> In some situations I'd like to import names inside a function, as
>> sometimes done in Python:
>>
>> int foo(int x) {
>>     import std.math: sqrt;
>>     return cast(int)(sqrt(cast(real)x) * 2);
>> }
> 
> Can't you just use 'with'?
> 
> static import std.math;
> 
> // Code...
> 
> int foo(int x) {
>     with(std.math) {
>        return cast(int)(sqrt(cast(real)x) * 2);
>     }
> }
> 
> Or was there another issue trying to be solved?

The other thing is to disable the visibility of the symbol outside the 
with scope. There are languages that have succesfully unified these two 
constructs. D isn't one of them.



More information about the Digitalmars-d mailing list