www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between using `import` with/without the colon

reply Jeremy <jtbx disroot.org> writes:
Hello, is there any difference at all between the following 
lines, as an example:

```d
import std.regex;
import std.regex : matchFirst;
```

What technical differences does it make (except for having the 
identifier available), using the colon?
Does it make any speed/optimization changes or am I better off 
just importing the whole module?
Mar 19 2023
parent reply Basile B. <b2.temp gmx.com> writes:
On Sunday, 19 March 2023 at 07:20:17 UTC, Jeremy wrote:
 Hello, is there any difference at all between the following 
 lines, as an example:

 ```d
 import std.regex;
 import std.regex : matchFirst;
 ```

 What technical differences does it make (except for having the 
 identifier available), using the colon?
 Does it make any speed/optimization changes or am I better off 
 just importing the whole module?
The colon-form, aka "selective import" has for effect 1. to create a local alias so this can indeed speedup symbol lookups in the sense that search will succeed before looking in the scope of the imports. 2. to make non-selected symbols, i.e not listed in the colon right hand side, in the import not available. Note that using both makes no sense, but I guess you did that to express more clearly what you meant.
Mar 19 2023
parent Jeremy <jtbx disroot.org> writes:
On Sunday, 19 March 2023 at 08:47:32 UTC, Basile B. wrote:
 On Sunday, 19 March 2023 at 07:20:17 UTC, Jeremy wrote:
 [...]
The colon-form, aka "selective import" has for effect 1. to create a local alias so this can indeed speedup symbol lookups in the sense that search will succeed before looking in the scope of the imports. 2. to make non-selected symbols, i.e not listed in the colon right hand side, in the import not available. Note that using both makes no sense, but I guess you did that to express more clearly what you meant.
Ah, that makes sense. Thank you!
Mar 19 2023