filtered imports

Jonathan Marler johnnymarler at gmail.com
Thu Sep 13 11:54:19 UTC 2018


"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)
-----------------------------------------------------



More information about the Digitalmars-d mailing list