writeln() in static import std

Steven Schveighoffer schveiguy at gmail.com
Thu Feb 13 15:25:40 UTC 2020


On 2/13/20 2:33 AM, Adnan wrote:
> How can I reach stdout.writeln() using fully qualified name with static 
> import?
> I have tried:
> std.stdio.stdout.writeln() -- fails
> std.writeln() -- works
> std.stdout.writeln -- works
> 
> 
> How does static import with std work?
Hm..., this works:

static import std.stdio;

void main()
{
     std.stdio.stdout.writeln("hi");
}

Did you do

static import std;

?

If so, this would explain your issues.

imports have some interesting behavior, but they are totally 
straightforward once you understand all the nuances.

static import imports the module as is, and provides all the symbols 
under the namespace named after the module. Thus, all the symbols 
imported with std are now available under in the namespace std.

What std does is import ALL MODULES in Phobos/druntime with public imports.

A public import imports all the symbols in the given module, and aliases 
all of them as if they were defined in the current module. Thus, you 
have the symbol std.stdout, but not the symbol std.stdio.stdout.

A selective import imports the given symbols and aliases them as if they 
were defined in the current module.

A renamed import imports the given module with the given alias 
substituting for the module imported.

So import io = std.stdio means you have io.stdio.writeln available.

Not sure if there's a good primer on how imports work, but the power 
available is quite good. You can make your namespace look like anything 
you want.

-Steve


More information about the Digitalmars-d-learn mailing list