www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hello, World!

reply Anonymous <im.anonymous some.mail> writes:
**Input:**
```d
void main()
{
     import std.stdio : writeln;
     writeln("Hello, World!\n");
     //I'm not sure if the ^^ is necessary or not
}
```

**Output:**
```
Hello, World!
```
That's it.
Mar 25 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
Welcome! :)

(We have a Learn forum (newsgroup) as well, where such posts are more 
suitable.)

On 3/25/22 13:07, Anonymous wrote:
 **Input:**
 ```d
 void main()
 {
      import std.stdio : writeln;
      writeln("Hello, World!\n");
      //I'm not sure if the ^^ is necessary or not
It may be useful if you want an additional empty line. ;) Otherwise, writeln is short form "write line", implying a newline will be printed at the end. (Conversely, write() does not add a newline.)
 }
 ```

 **Output:**
 ```
 Hello, World!
 ```
 That's it.
I like it! :) Ali
Mar 25 2022
parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Friday, 25 March 2022 at 20:29:22 UTC, Ali ร‡ehreli wrote:
 It may be useful if you want an additional empty line. ;)

 Otherwise, writeln is short form "write line", implying a 
 newline will be printed at the end. (Conversely, write() does 
 not add a newline.)
If a website was used to compile/output data, it might have truncated the newline(s). HTML afterall doesn't honor whitespacing. If it were **pre**formatted aka <pre> then would honor the text formatting. Using octal dump you could see the actual output. ``` $ ./test.exe | od -t c 0000000 H e l l o , W o r l d ! \r \n \r 0000020 \n ```
Mar 25 2022
prev sibling next sibling parent Anonymous <im.anonymous some.mail> writes:
On Hello, World! at some time, Anonymous wrote:
 **Input:**
 ```d
 void main()
 {
     import std.stdio : writeln;
     writeln("Hello, World!\n");
     //I'm not sure if the ^^ is necessary or not
 }
 ```

 **Output:**
 ```
 Hello, World!
 ```
 That's it.
Maybe this could be an added example for the example box found in [the dlang homepage](https://dlang.org)
Mar 25 2022
prev sibling next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 25 March 2022 at 20:07:39 UTC, Anonymous wrote:
 ```d
 void main()
 {
     import std.stdio : writeln;
     writeln("Hello, World!\n");
 }
 ```
Thank you... I think this is a work of art. Just like the banana work that was attached to the wall with duct tape, which Italian sculptor Maurizio Cattelan called "Comedy". Also, the end-of-line character is needed. In this way, there is the same number of characters as the line above. But I like this more: ```d void main() { import std.stdio : writefln; "Hello".writefln!"%s, World!"; } ``` Because it's functional... SDB 79
Mar 25 2022
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Saturday, 26 March 2022 at 04:03:08 UTC, Salih Dincer wrote:
 Thank you...

 I think this is a work of art.  Just like the banana work that 
 was attached to the wall with duct tape, which Italian sculptor 
 Maurizio Cattelan called "Comedy".

 Also, the end-of-line character is needed.  In this way, there 
 is the same number of characters as the line above. But I like 
 this more:

 ```d
      void main()
      {
           import std.stdio : writefln;
           "Hello".writefln!"%s, World!";
      }
 ```

 Because it's functional...

 SDB 79
Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
Mar 27 2022
next sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote:

 Throwing my hat in the ring:

 ```d
 import std.stdio;
 int main()
     => "Hello World".puts;
 ```
Report failure in case of success? :) (`puts` returns a "non-negative value" on success, which, with that wording, is not necessarily 0). Cute though.
Mar 28 2022
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 3/28/22 06:33, Stanislav Blinov wrote:
 On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote:

 Throwing my hat in the ring:

 ```d
 import std.stdio;
 int main()
     => "Hello World".puts;
 ```
Report failure in case of success? :)
We have precedent in rt_init: :) https://dlang.org/phobos/core_runtime.html#.rt_init It is not obvious from its documented "returns 1/0" but it is a translation of Runtime.initialize's true/false: https://dlang.org/phobos/core_runtime.html#.Runtime.initialize Ali
Mar 28 2022
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Mar 28, 2022 at 01:33:55PM +0000, Stanislav Blinov via Digitalmars-d
wrote:
 On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote:
 
 Throwing my hat in the ring:
 
 ```d
 import std.stdio;
 int main()
     => "Hello World".puts;
 ```
Report failure in case of success? :) (`puts` returns a "non-negative value" on success, which, with that wording, is not necessarily 0). Cute though.
This should fix the return code: import std.stdio; int main() => !(1 + "Hello World".puts); :-P (Well OK, this is a hack, EOF is not necessarily -1. But it is on my system.) T -- The computer is only a tool. Unfortunately, so is the user. -- Armaphine, K5
Mar 28 2022
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 28.03.22 16:34, H. S. Teoh wrote:
 
 	import std.stdio;
 	int main() => !(1 + "Hello World".puts);
int main() => !~"Hello World".puts;
Mar 29 2022
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Tuesday, 29 March 2022 at 07:06:57 UTC, Timon Gehr wrote:
 On 28.03.22 16:34, H. S. Teoh wrote:
 
 	import std.stdio;
 	int main() => !(1 + "Hello World".puts);
int main() => !~"Hello World".puts;
Or simply ```int main() => "Hello World".puts < 0;```
Mar 29 2022
next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 29 March 2022 at 07:25:57 UTC, Max Samukha wrote:
 Or simply
 ```d
 int main() => "Hello World".puts < 0;
 ```
For starters, I'd like to point out that it's essentially the same as this: ```d import std.stdio; //int main() => "Hello World".puts < 0;/* int main() { "Hello World".puts; return 0; }//*/ ```
Mar 29 2022
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Tuesday, 29 March 2022 at 09:25:56 UTC, Salih Dincer wrote:
 On Tuesday, 29 March 2022 at 07:25:57 UTC, Max Samukha wrote:
 Or simply
 ```d
 int main() => "Hello World".puts < 0;
 ```
For starters, I'd like to point out that it's essentially the same as this: ```d import std.stdio; //int main() => "Hello World".puts < 0;/* int main() { "Hello World".puts; return 0; }//*/ ```
No. Your variant loses error status and always reports success.
Mar 29 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Tuesday, 29 March 2022 at 09:29:30 UTC, Stanislav Blinov wrote:
 On Tuesday, 29 March 2022 at 09:25:56 UTC, Salih Dincer wrote:
 On Tuesday, 29 March 2022 at 07:25:57 UTC, Max Samukha wrote:
 Or simply
 ```d
 int main() => "Hello World".puts < 0;
 ```
For starters, I'd like to point out that it's essentially the same as this: ```d import std.stdio; //int main() => "Hello World".puts < 0;/* int main() { "Hello World".puts; return 0; }//*/ ```
No. Your variant loses error status and always reports success.
Does it matter ๐Ÿ˜€ SDB 79
Mar 29 2022
parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Tuesday, 29 March 2022 at 10:12:13 UTC, Salih Dincer wrote:
 On Tuesday, 29 March 2022 at 09:29:30 UTC, Stanislav Blinov 
 wrote:
 Your variant loses error status and always reports success.
Does it matter ๐Ÿ˜€
Presumably it does if we're talking about a returning `main`. Otherwise you can simply define a `void main` and not spell out the `return` at all, i.e. as in original post. Although, the OP variant would throw if printing errors out (because it uses `writeln` and not `puts`). So... yeah, it does? :D
Mar 29 2022
prev sibling parent reply Andrea Fontana <nospam example.com> writes:
On Tuesday, 29 March 2022 at 07:25:57 UTC, Max Samukha wrote:
 On Tuesday, 29 March 2022 at 07:06:57 UTC, Timon Gehr wrote:
 On 28.03.22 16:34, H. S. Teoh wrote:
 
 	import std.stdio;
 	int main() => !(1 + "Hello World".puts);
int main() => !~"Hello World".puts;
Or simply ```int main() => "Hello World".puts < 0;```
Why not ```pragma(msg, "Hello World");```
Mar 30 2022
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 30 March 2022 at 08:13:48 UTC, Andrea Fontana wrote:

 Why not ```pragma(msg, "Hello World");```
Because pragma(msg) is an inglorious hack. We need proper I/O at compile time.
Mar 30 2022
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Mar 30, 2022 at 08:34:09AM +0000, Max Samukha via Digitalmars-d wrote:
 On Wednesday, 30 March 2022 at 08:13:48 UTC, Andrea Fontana wrote:
 
 
 Why not ```pragma(msg, "Hello World");```
Because pragma(msg) is an inglorious hack. We need proper I/O at compile time.
And pragma(msg) is technically not your program outputting the message, but the compiler. You need to put it in main() for a proper implementation of Hello World. ;-) T -- It only takes one twig to burn down a forest.
Mar 30 2022
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 30 March 2022 at 15:49:08 UTC, H. S. Teoh wrote:

 And pragma(msg) is technically not your program outputting the 
 message,
 but the compiler. You need to put it in main() for a proper
 implementation of Hello World. ;-)


 T
template add(int x) { pragma(msg, x); int add(int y) { import std.stdio: writeln; writeln(y); return x + y; } } void main() { auto z = add!1(2); } Is this a program that starts at compile time and continues at run time, or a meta-program that generates another program? )
Mar 30 2022
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Mar 30, 2022 at 06:50:40PM +0000, Max Samukha via Digitalmars-d wrote:
 On Wednesday, 30 March 2022 at 15:49:08 UTC, H. S. Teoh wrote:
[...]
 And pragma(msg) is technically not your program outputting the
 message, but the compiler. You need to put it in main() for a proper
 implementation of Hello World. ;-)
[...]
 template add(int x)
 {
     pragma(msg, x);
     int add(int y)
     {
         import std.stdio: writeln;
         writeln(y);
         return x + y;
     }
 }
 
 void main()
 {
     auto z = add!1(2);
 }
 
 Is this a program that starts at compile time and continues at run
 time, or a meta-program that generates another program? )
[...] It's a program with a split personality that prints half its output at compile time and the other half at runtime. :-D If you only compile but never run it, then you only get half of its output, and if you run it multiple times you get redundant output. :-P T -- The two rules of success: 1. Don't tell everything you know. -- YHL
Mar 30 2022
parent Max Samukha <maxsamukha gmail.com> writes:
On Wednesday, 30 March 2022 at 20:05:07 UTC, H. S. Teoh wrote:

 It's a program with a split personality that prints half its 
 output at compile time and the other half at runtime. :-D

 If you only compile but never run it, then you only get half of 
 its output, and if you run it multiple times you get redundant 
 output. :-P


 T
That's the way to think about it. Thanks! )
Apr 05 2022
prev sibling parent reply Tejas <notrealemail gmail.com> writes:
On Wednesday, 30 March 2022 at 18:50:40 UTC, Max Samukha wrote:
 On Wednesday, 30 March 2022 at 15:49:08 UTC, H. S. Teoh wrote:

 And pragma(msg) is technically not your program outputting the 
 message,
 but the compiler. You need to put it in main() for a proper
 implementation of Hello World. ;-)


 T
template add(int x) { pragma(msg, x); int add(int y) { import std.stdio: writeln; writeln(y); return x + y; } } void main() { auto z = add!1(2); } Is this a program that starts at compile time and continues at run time, or a meta-program that generates another program? )
It's a normal template... what are you talking about?? ```d 1 2 3 import object; template add(int x) { pragma (msg, x); int add(int y) { return x + y; } } extern (C) extern (C) void main() { add(2); add(1); add(0); return 0; } add!1 { pure nothrow nogc safe int add(int y) { return 1 + y; } } add!2 { pure nothrow nogc safe int add(int y) { return 2 + y; } } add!3 { pure nothrow nogc safe int add(int y) { return 3 + y; } } ``` (Took the output from the "AST" option in run.dlang.io) (Used `-betterC` to strip away boilerplate stuff)
Mar 30 2022
parent Max Samukha <maxsamukha gmail.com> writes:
On Thursday, 31 March 2022 at 03:44:30 UTC, Tejas wrote:
 It's a normal template... what are you talking about??
Or something like this https://en.wikipedia.org/wiki/Partial_evaluation
Apr 05 2022
prev sibling next sibling parent Anonymous <im.anonymous some.mail> writes:
On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote:
 On Saturday, 26 March 2022 at 04:03:08 UTC, Salih Dincer wrote:
 Thank you...

 I think this is a work of art.  Just like the banana work that 
 was attached to the wall with duct tape, which Italian 
 sculptor Maurizio Cattelan called "Comedy".

 Also, the end-of-line character is needed.  In this way, there 
 is the same number of characters as the line above. But I like 
 this more:

 ```d
      void main()
      {
           import std.stdio : writefln;
           "Hello".writefln!"%s, World!";
      }
 ```

 Because it's functional...

 SDB 79
Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
2 line version ```d import std.stdio; void main() { "Hello World".writeln; } // Or "puts" ```
Mar 28 2022
prev sibling parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote:
 On Saturday, 26 March 2022 at 04:03:08 UTC, Salih Dincer wrote:
 Thank you...

 I think this is a work of art.  Just like the banana work that 
 was attached to the wall with duct tape, which Italian 
 sculptor Maurizio Cattelan called "Comedy".

 Also, the end-of-line character is needed.  In this way, there 
 is the same number of characters as the line above. But I like 
 this more:

 ```d
      void main()
      {
           import std.stdio : writefln;
           "Hello".writefln!"%s, World!";
      }
 ```

 Because it's functional...

 SDB 79
Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
```d void main() => imported!`std`.writeln("Hello World"); ``` :P
Mar 29 2022
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Tuesday, 29 March 2022 at 09:36:20 UTC, Petar Kirov 
[ZombineDev] wrote:

 ```d
 void main() => imported!`std`.writeln("Hello World");
 ```

 :P
Where's the import importing imported? )
Mar 29 2022
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 29.03.22 13:56, Max Samukha wrote:
 On Tuesday, 29 March 2022 at 09:36:20 UTC, Petar Kirov [ZombineDev] wrote:
 
 ```d
 void main() => imported!`std`.writeln("Hello World");
 ```

 :P
Where's the import importing imported? )
https://github.com/dlang/dmd/blob/master/src/dmd/dmodule.d#L1139 https://github.com/dlang/druntime/blob/master/src/object.d#L5087
Mar 29 2022
parent reply Max Samukha <maxsamukha gmail.com> writes:
On Tuesday, 29 March 2022 at 12:52:17 UTC, Timon Gehr wrote:

 
 Where's the import importing imported? )
 
https://github.com/dlang/dmd/blob/master/src/dmd/dmodule.d#L1139 https://github.com/dlang/druntime/blob/master/src/object.d#L5087
Nice! I didn't know it'd been added to object.d.
Mar 29 2022
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 29 March 2022 at 13:49:41 UTC, Max Samukha wrote:
 [snip]

 Nice! I didn't know it'd been added to object.d.
Same. I can't recall, but in the examples where they have imported!"std.XXX".YYY, am I right that it all of XXX for that declaration?
Mar 29 2022
parent reply Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Tuesday, 29 March 2022 at 14:00:20 UTC, jmh530 wrote:
 On Tuesday, 29 March 2022 at 13:49:41 UTC, Max Samukha wrote:
 [snip]

 Nice! I didn't know it'd been added to object.d.
Same. I can't recall, but in the examples where they have imported!"std.XXX".YYY, am I right that it all of XXX for that declaration?
`imported!"XXX".YYY` imports the symbol `YYY` from the module named `XXX`. It doesn't use selective imports (although I think it could with `opDispatch`), so the whole module named `XXX` will be imported. `XXX` could be `std.algorithm.iteration`, but also `mir.ndslice`, `vibe.core.net`, or any other module from a D library (of course, assuming you have that third-party library installed somehow, say with Dub). In the case of Phobos, [there's a convenience module called `std`][1] that publicly imports most of Phobos, so you simply say `imported!"std".map`, instead of `imported!"std.algorithm".map`, or `imported!"std.algorithm.iteration".map`. Of course, importing the whole Phobos will make your compilation slower, but for now that's the price to pay for a bit of convenience. Even though some regard `import std;` as an anit-pattern, I think it's a nice feature and it's the compiler's job to make it fast (e.g. assuming the Phobos source files rarely change, the compiler could generate an import cache file to make look ups faster). [1]: https://github.com/dlang/phobos/blob/v2.099.0/std/package.d#L4
Mar 29 2022
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 29 March 2022 at 16:23:12 UTC, Petar Kirov 
[ZombineDev] wrote:
 [snip]

 `imported!"XXX".YYY` imports the symbol `YYY` from the module 
 named `XXX`. It doesn't use selective imports (although I think 
 it could with `opDispatch`), so the whole module named `XXX` 
 will be imported. `XXX` could be `std.algorithm.iteration`, but 
 also `mir.ndslice`, `vibe.core.net`, or any other module from a 
 D library (of course, assuming you have that third-party 
 library installed somehow, say with Dub).

 [snip]
Thanks!
Mar 29 2022
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Mar 29, 2022 at 04:23:12PM +0000, Petar via Digitalmars-d wrote:
[...]
 Even though some regard `import std;` as an anit-pattern, I think it's
 a nice feature and it's the compiler's job to make it fast (e.g.
 assuming the Phobos source files rarely change, the compiler could
 generate an import cache file to make look ups faster).
[...] To add more substance to this discussion, I decided to make some real measurements on the impact of `import std;`. The following timings were obtained by compiling the same input file 100 times with dmd. The loop was done in the shell, so there's likely to be some shell overhead, but I'm making the assumption that this is negligible. // Empty main() with no imports (base case) real 0m12.830s user 0m9.970s sys 0m2.822s // Empty main() with `import std;` real 1m21.429s user 1m7.603s sys 0m13.529s // Empty main() with `import std.stdio;` real 0m17.745s user 0m13.888s sys 0m3.793s // main() prints "Hello, World!" with `import std.stdio;` real 0m20.710s user 0m16.317s sys 0m4.316s // main() prints "Hello, World!" with `import std;` real 1m21.547s user 1m7.709s sys 0m13.549s The impact of `import std;` is quite apparent. It adds about a minute to the overall compile time, over a more specific `import std.stdio;`. Invoking `writeln` adds only 3 seconds to the measurement, negligible. Though keep in mind that these measurements are magnified 100x, so the actual impact of `import std;` (when compiling only once) is only about 0.8s. // I was curious about how this compares with the impact of using some of the well-known heavyweight template functions like writefln. So here are some further measurements: // `import std.stdio;` with `writefln("Hello, %s!", "World");` real 0m58.014s user 0m48.912s sys 0m8.858s // `import std;` with `writefln("Hello, %s!", "World");` real 1m33.955s user 1m19.148s sys 0m14.460s Interestingly enough, the impact of `import std;` vs `import std.stdio;` seems narrower here. The instantiation of writefln added about 30-40 seconds to the measurement in the `import std.stdio;` case, but seems to have less impact in the `import std;` case. This suggests that in non-trivial template-heavy code, the cost of instantiating templates may quickly overshadow the overhead of `import std;`, which jives with my own experience in my recent projects (`import std;` is just so darned convenient I have little motivation for writing specific imports!). T -- Life is unfair. Ask too much from it, and it may decide you don't deserve what you have now either.
Mar 29 2022
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 29 March 2022 at 17:13:14 UTC, H. S. Teoh wrote:
 [snip]
Interesting. Thanks. I know it's annoying to endless produce performance comparisons, but it would be interesting to see the impact of selective imports and whether the imported template as in below has an impact. ``` void main() { with(imported!"std.stdio") { writeln("Hello, World!"); } } ```
Mar 29 2022
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Mar 29, 2022 at 05:52:14PM +0000, jmh530 via Digitalmars-d wrote:
[...]
 I know it's annoying to endless produce performance comparisons, but
 it would be interesting to see the impact of selective imports and
 whether the imported template as in below has an impact.
 ```
 void main() {
     with(imported!"std.stdio") {
         writeln("Hello, World!");
     }
 }
 ```
Using `with(imported!"std.stdio")`, printing "Hello, World!": real 0m20.430s user 0m16.199s sys 0m4.153s Basically, indistinguishible from the global `import std.stdio;` case. (I consider sub-second differences as noise in the measurement, it's not significant.) T -- BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL
Mar 29 2022
prev sibling parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Friday, 25 March 2022 at 20:07:39 UTC, Anonymous wrote:
 **Input:**
 ```d
 void main()
 {
     import std.stdio : writeln;
     writeln("Hello, World!\n");
     //I'm not sure if the ^^ is necessary or not
 }
Not efficient enough. :^) ```d extern (C) { int write(int, immutable char*, int); } void main() { write(1,&"Hello, world!\n"[0],14); } ```
Apr 05 2022