imports in functions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jun 12 14:25:43 PDT 2011


On 06/12/2011 04:15 PM, Walter Bright wrote:
> Nobody seems to have noticed yet, but yesterday I removed the
> restriction preventing import declarations from being used in functions.
> These now work:
>
> void test1()
> {
> import std.c.stdio;
> printf("hello world\n");
> }
>
> void test2()
> {
> static import std.c.stdio;
> std.c.stdio.printf("hello world\n");
> }
>
> https://github.com/D-Programming-Language/dmd/commit/d5fbd53aa8d8452dce2514944575e654d387477a
>
>
> I believe this can lead to better encapsulation of code, especially when
> using mixins, versioning and other conditional compilation constructs.

Fabulous. std.benchmark already uses the feature, and from a mixin no less:

https://github.com/andralex/phobos/commit/c5f2778a0fc393d6bd17ebec4765b28937575c31

One interesting aspect of the feature is that if import is used from 
within a template, it's not actually imported unless the template is 
instantiated.

Andrei


More information about the Digitalmars-d mailing list