Function-local imports

Jesse Phillips jessekphillips+D at gmail.com
Sun Apr 25 21:40:09 PDT 2010


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?




More information about the Digitalmars-d mailing list