Early review of std.logger

Jacob Carlborg doob at me.com
Thu Nov 7 01:09:13 PST 2013


On 2013-11-06 14:21, Dicebot wrote:
> On Tuesday, 5 November 2013 at 08:38:34 UTC, Robert Schadek wrote:
>> Can you give an example, I'm not sure if I know what you mean.
>
> Currently common approach is just adding top-level module imports like
> this:
>
> ```
> module app;
> import std.algorithm;
> ```
>
> It has several issues:
>
> 1) Name clash if two modules define same symbol
> 2) Difficulties with tracking unused imports
> 3) Compilation speed penalty because of eager import in case it is only
> used in uninstantiated code branch.
>
> However, D has 3 totally awesome import features that help to address
> all of this issues:
>
> 1) aliasing imports
> ```
> import algo = std.algorithm;
> ```
>
> It creates the very same namespace one is trying to achieve with
> namespace struct but without extra unused symbols and with full user
> control. Benefits both readability of application (obvious where
> namespace comes from) and readability of imported module (less redundant
> nesting)
>
> 2) importing specific symbols
> ```
> import std.algorithm : remove;
> ```
>
> Often module is used only for one or two functions. Mentioning those
> explicitly will avoid accidental clashes with irrelevant module symbols
> and acts as a nice self-documentation tool.
>
> 3) scope-local imports
> ```
> template tmpl(T)
> {
>      import std.algorithm : remove;
> }
> ```

4) static imports

static import std.stdio;

void main ()
{
     std.stdio.writeln("foo"); // fully qualified name is required
}

2 + 3) combining selective and renamed imports

import io = std.stdio : println = writeln;

void main ()
{
     println("foo");
     io.writeln("bar");
}

6) aliases

import std.stdio;
alias println = writeln;

void main ()
{
     println("foo");
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list