filtered imports
rikki cattermole
rikki at cattermole.co.nz
Thu Sep 13 11:58:40 UTC 2018
On 13/09/2018 11:54 PM, Jonathan Marler wrote:
> "Selective imports" limit the symbols imported from a module by
> providing a list of all the symbols to include:
>
> import std.stdio : writeln, writefln;
>
> The complement of this would be a "Filtered import", meaning, import all
> the symbols except the ones in the provided list. I imagine the syntax
> would look something like:
>
> import std.stdio ~ writeln, writefln;
>
>
> To provide a use case for this, say you have a module that uses a fair
> amount of symbols from `std.file` but you want to make sure that all
> calls to `chdir` are logged. Using a filtered import would allow you to
> exclude `chdir` from being available globally so you could create a
> wrapper that all code is forced to go through.
>
> import std.stdio;
> import std.file ~ chdir;
>
> void chdir(R)(R path)
> {
> writefln("chdir '%s'", path);
> from!"std.file".chdir(path);
> }
>
> It's an interesting variation on D's current repertoire of import
> semantics. Possibly worth consideration as an addition to the language.
>
> Grammar Changes:
> -----------------------------------------------------
> ImportBindings: (existing rule)
> Import : ImportBindList (existing rule)
> Import ~ ImportExcludeList (new rule)
>
> ImportExcludeList: (new rule)
> Identifier, ImportExcludeList (new rule)
> -----------------------------------------------------
import std.stdio;
import std.file;
void chdir(R)(R path) {
writeln("changing dir to ", path);
std.file.chdir(path);
}
void main()
{
chdir("/tmp");
}
More information about the Digitalmars-d
mailing list