www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiple selective imports on one line

reply earthfront <earthfront safetymail.info> writes:
I'm using hackerpilot's excellent textadept plugin + DCD, Dfmt, 
and Dscanner.
Upon saving files, it produces suggestions, much like warnings 
from the compiler.

One suggestion is to use selective imports in local scopes. OK, 
I'll do that.

Now I'm left with a smattering of lines which are just selective 
imports from a single module:
void foo()
{
   import std.exception:enforce;
   import std.algorithm:array;
   import std.algorithm.iteration:filter;
   import std.functional:memoize;

   //..Work..
}

What is the proper way to combine these into one line?
Dec 23 2015
next sibling parent reply Jakob Ovrum <jakobovrum gmail.com> writes:
On Wednesday, 23 December 2015 at 10:51:52 UTC, earthfront wrote:
 I'm using hackerpilot's excellent textadept plugin + DCD, Dfmt, 
 and Dscanner.
 Upon saving files, it produces suggestions, much like warnings 
 from the compiler.

 One suggestion is to use selective imports in local scopes. OK, 
 I'll do that.

 Now I'm left with a smattering of lines which are just 
 selective imports from a single module:
 void foo()
 {
   import std.exception:enforce;
   import std.algorithm:array;
   import std.algorithm.iteration:filter;
   import std.functional:memoize;

   //..Work..
 }

 What is the proper way to combine these into one line?
There's no `array` in std.algorithm, and I don't see more than one import per module in your example, but I'm guessing what you want is: import mod : a, b, c; // import a, b and c from mod
Dec 23 2015
parent reply earthfront <earthfront safetymail.info> writes:
On Wednesday, 23 December 2015 at 11:00:19 UTC, Jakob Ovrum wrote:
 On Wednesday, 23 December 2015 at 10:51:52 UTC, earthfront 
 wrote:
 Now I'm left with a smattering of lines which are just 
 selective imports from a single module:
   import std.exception:enforce;
   import std.algorithm:array;
   import std.algorithm.iteration:filter;
   import std.functional:memoize;

 What is the proper way to combine these into one line?
There's no `array` in std.algorithm, and I don't see more than one import per module in your example, but I'm guessing what you want is: import mod : a, b, c; // import a, b and c from mod
Sorry, no, I didn't want that. I should have typed above: "I'm left with lines which are selective imports from various modules." My goal is to import several symbols from different modules on one line. I'm trying to figure out if it's possible or not. It makes the code more concise in some cases.
Dec 23 2015
parent reply Basile B. <b2.temp gmx.com> writes:
On Wednesday, 23 December 2015 at 19:34:26 UTC, earthfront wrote:
 On Wednesday, 23 December 2015 at 11:00:19 UTC, Jakob Ovrum 
 wrote:
 [...]

 My goal is to import several symbols from different modules on 
 one line.
 I'm trying to figure out if it's possible or not. It makes the 
 code more concise in some cases.
This is not available in the grammar. You can still open a duplicate enhancement request. https://issues.dlang.org/show_bug.cgi?id=14704 see also: http://forum.dlang.org/post/trrxoacvpyyqrdfqxcvx forum.dlang.org I remember also another NG thread about this. A guy wanted this feature because he thought that this would allow him to write faster during a Hackathlon or something like that.
Dec 23 2015
parent earthfront <earthfront safetymail.info> writes:
On Wednesday, 23 December 2015 at 20:56:39 UTC, Basile B. wrote:
 This is not available in the grammar. You can still open a 
 duplicate enhancement request.

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

 see also:

 http://forum.dlang.org/post/trrxoacvpyyqrdfqxcvx forum.dlang.org
OK. I'm putting in my vote for this, and maybe even write up a pull request. The value from this comes when using "component-oriented" programming, as touted by AA and WB: http://www.drdobbs.com/architecture-and-design/component-programming-in-d/240008321 The combination of the good practice of local selective imports, and the use of various library functions to perform component oriented programming, results in verbose local "import" statements. View the example below. The import section is nearly as dense as the component oriented block doing the actual work. This is smelly. <CODE> void main( string args[] ) { import std.exception: enforce; import std.array: array; import std.algorithm.iteration: map,filter; import std.string: removechars; //..snip ..// auto file = File( args[1] ); auto result = file.byLineCopy( KeepTerminator.no, ',') .array .map!(a => removechars!string( a, "\"" ) ) .filter!isTriangleWord .array .length; writeln("Number of triangle words: ", result); } </CODE> Understandably, this isn't a huge priority, but a spot for improvement none-the-less. Dennis M. Ritchie's design in his comment seems to be the most concise: import {std.array:array; std.algorithm.iteration:map,filter};
Dec 24 2015
prev sibling next sibling parent reply ZombineDev <valid_email he.re> writes:
On Wednesday, 23 December 2015 at 10:51:52 UTC, earthfront wrote:
 I'm using hackerpilot's excellent textadept plugin + DCD, Dfmt, 
 and Dscanner.
 Upon saving files, it produces suggestions, much like warnings 
 from the compiler.

 One suggestion is to use selective imports in local scopes. OK, 
 I'll do that.

 Now I'm left with a smattering of lines which are just 
 selective imports from a single module:
 void foo()
 {
   import std.exception:enforce;
   import std.algorithm:array;
   import std.algorithm.iteration:filter;
   import std.functional:memoize;

   //..Work..
 }

 What is the proper way to combine these into one line?
Actually array() is from sts.array and correct way to use selective imports is: import std.exception : enforce; import std.array : array; import std.algorithm.iteration : filter; import std.functional : memoize; If you want to import several symbols from one module, you can list them after the column like so: import std.algorithm : any, find, sort /*, etc... */;
Dec 23 2015
parent reply earthfront <earthfront safetymail.info> writes:
On Wednesday, 23 December 2015 at 11:12:22 UTC, ZombineDev wrote:
 Actually array() is from sts.array and correct way to use 
 selective imports is:
 import std.exception : enforce;
 import std.array : array;
 import std.algorithm.iteration : filter;
 import std.functional : memoize;

 If you want to import several symbols from one module, you can 
 list them after the column like so:
 import std.algorithm : any, find, sort /*, etc... */;
I hadn't compiled yet, so the array thing is indeed an error. So there's no way to combine these _selective_ imports on a single line, like I can with regular _module_ imports: import std.exception, std.array, std.functional; Correct?
Dec 23 2015
parent ZombineDev <valid_email he.re> writes:
On Wednesday, 23 December 2015 at 19:27:31 UTC, earthfront wrote:
 On Wednesday, 23 December 2015 at 11:12:22 UTC, ZombineDev 
 wrote:
 Actually array() is from sts.array and correct way to use 
 selective imports is:
 import std.exception : enforce;
 import std.array : array;
 import std.algorithm.iteration : filter;
 import std.functional : memoize;

 If you want to import several symbols from one module, you can 
 list them after the column like so:
 import std.algorithm : any, find, sort /*, etc... */;
I hadn't compiled yet, so the array thing is indeed an error. So there's no way to combine these _selective_ imports on a single line, like I can with regular _module_ imports: import std.exception, std.array, std.functional; Correct?
Yes, it is not allowed by the grammar. I think the reason is that the list items would become ambiguous (the difference between selected symbols and module names could only be revealed after semantic analysis). See for example: import abc : cba, dcb, bca : acb, dbc; // What is dbc? A module or a member of bca?
Dec 23 2015
prev sibling parent reply Joakim <dlang joakim.fea.st> writes:
On Wednesday, 23 December 2015 at 10:51:52 UTC, earthfront wrote:
 I'm using hackerpilot's excellent textadept plugin + DCD, Dfmt, 
 and Dscanner.
 Upon saving files, it produces suggestions, much like warnings 
 from the compiler.

 One suggestion is to use selective imports in local scopes. OK, 
 I'll do that.

 Now I'm left with a smattering of lines which are just 
 selective imports from a single module:
 void foo()
 {
   import std.exception:enforce;
   import std.algorithm:array;
   import std.algorithm.iteration:filter;
   import std.functional:memoize;

   //..Work..
 }

 What is the proper way to combine these into one line?
You really shouldn't have to do this by hand. I wish dfmt could do this for us, so that you develop with all the modules imported at the top, then run dfmt and it scopes all the imports and adds the selective import of symbols. I've been thinking about implementing a tool to do this myself, will get around to it someday.
Dec 28 2015
parent reply Basile B. <b2.temp gmx.com> writes:
On Monday, 28 December 2015 at 14:09:30 UTC, Joakim wrote:
 I wish dfmt could do this for us, so that you develop with all 
 the modules imported at the top, then run dfmt and it scopes 
 all the imports and adds the selective import of symbols.  I've 
 been thinking about implementing a tool to do this myself, will 
 get around to it someday.
This is not formating (what DFMT is aimed to do) this is refactoring.
Dec 28 2015
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Monday, 28 December 2015 at 14:16:36 UTC, Basile B. wrote:
 On Monday, 28 December 2015 at 14:09:30 UTC, Joakim wrote:
 I wish dfmt could do this for us, so that you develop with all 
 the modules imported at the top, then run dfmt and it scopes 
 all the imports and adds the selective import of symbols.  
 I've been thinking about implementing a tool to do this 
 myself, will get around to it someday.
This is not formating (what DFMT is aimed to do) this is refactoring.
oops, my answer could lead to a misunderstanding. I meant: This is not formating (what DFMT is aimed to do), but rather refactoring.
Dec 28 2015
prev sibling parent Joakim <dlang joakim.fea.st> writes:
On Monday, 28 December 2015 at 14:16:36 UTC, Basile B. wrote:
 On Monday, 28 December 2015 at 14:09:30 UTC, Joakim wrote:
 I wish dfmt could do this for us, so that you develop with all 
 the modules imported at the top, then run dfmt and it scopes 
 all the imports and adds the selective import of symbols.  
 I've been thinking about implementing a tool to do this 
 myself, will get around to it someday.
This is not formating (what DFMT is aimed to do) this is refactoring.
You're right, I was thinking about patching Dscanner to do it, just mentioned dfmt here without thinking about that aspect.
Dec 28 2015