www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there an alias for standard libraries to use in import statement?

reply BoQsc <vaidas.boqsc gmail.com> writes:
I just started with a fresh look at the D language and would like 
to be able to rewrite this code:

 import std;
 void main()
 {
     writeln("Hello D");
 }
 
Into more readable standard library name:
 
 import system;
 void main()
 {
     writeln("Hello D");
 }
Or into this
 import library.standard;
 void main()
 {
     writeln("Hello D");
 }
Or into this:
 import library phobos;
 void main()
 {
     writeln("Hello D");
 }
These were the examples that might feel more readable and natural than simply a three letter junction:
 import std;
What do you think?
Jul 04 2021
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Sunday, 4 July 2021 at 07:40:44 UTC, BoQsc wrote:
 I just started with a fresh look at the D language and would 
 like to be able to rewrite this code:

 import std;
 void main()
 {
     writeln("Hello D");
 }
 
Into more readable standard library name:
 
 import system;
 void main()
 {
     writeln("Hello D");
 }
You can use named imports, but then you have to use the name as a namespace: ``` import system = std; void main() { system.writeln("Hello D"); } ```
 These were the examples that might feel more readable and 
 natural than simply a three letter junction:
 import std;
What do you think?
If you're worried about the readability of `std` by itself, don't use it by itself. Import the specific modules you want: ``` import std.algorithm, std.range, std.stdio; ``` or whatever. It's a non-issue, IMO. It's not like anyone using D doesn't know what std is.
Jul 04 2021
parent BoQsc <vaidas.boqsc gmail.com> writes:
On Sunday, 4 July 2021 at 08:50:54 UTC, Mike Parker wrote:
 You can use named imports, but then you have to use the name as 
 a namespace:

 ```
 import system = std;
 void main()
 {
     system.writeln("Hello D");
 }
 ```

 These were the examples that might feel more readable and 
 natural than simply a three letter junction:
 import std;
What do you think?
If you're worried about the readability of `std` by itself, don't use it by itself. Import the specific modules you want: ``` import std.algorithm, std.range, std.stdio; ```
Thank you for showing the alternative.
 or whatever. It's a non-issue, IMO. It's not like anyone using 
 D doesn't know what std is.
It's not about knowing or not knowing, it's about the reading flow and ease on the thinking. For me, it's always better to have things spelled correctly. Constant abbrevations do feel unnatural to me, even if I'm familiar of the complexity behind it. Anyways, even if it is possible as you shown. I'm feeling like it's still non standard way and might confuse people when the whole D code base with community is about importing std library directly. But I'm glad I won't have to think about this problem anymore and that's important to have less important questions answered for more important things to accomplish.
Jul 04 2021
prev sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Sunday, 4 July 2021 at 07:40:44 UTC, BoQsc wrote:
 I just started with a fresh look at the D language and would 
 like to be able to rewrite this code:

 import std;
 void main()
 {
     writeln("Hello D");
 }
 
Into more readable standard library name:
 
 import system;
 void main()
 {
     writeln("Hello D");
 }
That is [easy](https://run.dlang.io/is/af8dMY), just define the `system` module and publicly `import std`: --- test.d ```d import system; void main() { writeln("Hello D"); } ``` --- system.d ```d module system; public import std; ``` But like Mike, I advise against this. It will confuse every D programmer that is trying to read your code. What is most natural depends heavily on your background and what language you are coming from. `std` is perfectly natural if you're coming from C++. Fun fact (but equally ill advised): You can hide the entire import from view like this: ```d void main() { writeln("Hello D"); } /* Lean on Enter for a while */ import std; ``` -- Bastiaan.
Jul 04 2021