www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13855] New: Allow multiple selective imports from different

https://issues.dlang.org/show_bug.cgi?id=13855

          Issue ID: 13855
           Summary: Allow multiple selective imports from different
                    modules in a single import statement
           Product: D
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: code dawg.eu

Couldn't we just allow this?

    import std.algorithm : copy, uniq, std.range : walkLength;

This is one of (very few) grammar warts that regularly annoy me, because I have
to make it 2 lines

    import std.algorithm : copy, uniq;
    import std.range : walkLength;

or import one module completely.

    import std.algorithm, std.range : walkLength;

Sure this would make the grammar slightly more ambiguous, but when peeking a
few tokens ahead you can always make a decision. We'd just have to disallow
that selective imports are followed by non-selective ones.

Especially since we found out that static and selective imports could be done
lazily (bug 13255), it would be nice.

------
To defeat a few expected counter-arguments upfront.

- "One line more or less doesn't matter."

This is a function that we recently added to dub [1].

auto fuzzySearch(R)(R strings, string input){
    import std.algorithm : levenshteinDistance, schwartzSort, partition3;
    import std.traits : isSomeString;
    import std.range : ElementType;

    static assert(isSomeString!(ElementType!R), "Cannot call fuzzy search on
non string rang");
    immutable threshold = input.length / 4;
    return strings.partition3!((a, b) => a.length + threshold <
b.length)(input)[1]
        .schwartzSort!(p => levenshteinDistance(input.toUpper, p.toUpper));
}

It could be written like so.

auto fuzzySearch(R)(R strings, string input){
    import std.algorithm : levenshteinDistance, schwartzSort, partition3,
        std.traits : isSomeString, std.range : ElementType;

    static assert(isSomeString!(ElementType!R), "Cannot call fuzzy search on
non string rang");
    immutable threshold = input.length / 4;
    return strings.partition3!((a, b) => a.length + threshold <
b.length)(input)[1]
        .schwartzSort!(p => levenshteinDistance(input.toUpper, p.toUpper));
}

Much better signal/noise ratio.

- "You could import the complete module because that's already local import."

Often you don't want to pollute your scope, even a small one.
At least I intuitively thinks of importing as an expenditure that should be
minimized. If we make selective imports lazy that would actually be true.
Also selective imports self-document where you got a certain symbol.

[1]:
https://github.com/D-Programming-Language/dub/blob/c67201d663f3cf832feed1c3894c3aec328fec1a/source/dub/internal/utils.d#L233

--
Dec 11 2014