I've been thinking about scoped imports. While it seems like you can use scoped imports a lot of the time, it seems like there are some potential cases where you can't. Most notably if a function input is a struct or class, then I can't figure out a way to use a scoped import. This also applies to template constraints I think.

I put some code below to illustrate what I mean. The local import has to apply to the entire app module in order for foo to work. It can't only apply to the foo function the way that the std.stdio import can. By contrast, bar uses a scoped import of S (which works even if the first import is removed), but it means that you can't pass s in.

Is there any way to use a scoped import of S with foo that I may have overlooked?

module local;

struct S { int a = 2; }

module app;

import local : S;

void foo(S s)
{
        import std.stdio : writeln;
        writeln(s.a);
}

void bar()
{
        import std.stdio : writeln;
        import local : S;
        S s;
        writeln(s.a);
}

void main()
{
        S s;
        foo(s);
        bar();
}

Reply via email to