digitalmars.D - Hello, World!
- Anonymous (14/14) Mar 25 2022 **Input:**
- =?UTF-8?Q?Ali_=c3=87ehreli?= (9/23) Mar 25 2022 Welcome! :)
- Era Scarecrow (12/16) Mar 25 2022 If a website was used to compile/output data, it might have
- Anonymous (3/17) Mar 25 2022 Maybe this could be an added example for the example box found in
- Salih Dincer (17/24) Mar 25 2022 Thank you...
- FeepingCreature (7/23) Mar 27 2022 Throwing my hat in the ring:
- Stanislav Blinov (4/10) Mar 28 2022 Report failure in case of success? :) (`puts` returns a
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/16) Mar 28 2022 We have precedent in rt_init: :)
- H. S. Teoh (9/22) Mar 28 2022 This should fix the return code:
- Timon Gehr (2/5) Mar 29 2022 int main() => !~"Hello World".puts;
- Max Samukha (2/7) Mar 29 2022 Or simply ```int main() => "Hello World".puts < 0;```
- Salih Dincer (11/15) Mar 29 2022 For starters, I'd like to point out that it's essentially the
- Stanislav Blinov (2/18) Mar 29 2022 No. Your variant loses error status and always reports success.
- Salih Dincer (3/21) Mar 29 2022 Does it matter ๐
- Stanislav Blinov (6/10) Mar 29 2022 Presumably it does if we're talking about a returning `main`.
- Andrea Fontana (2/10) Mar 30 2022 Why not ```pragma(msg, "Hello World");```
- Max Samukha (3/4) Mar 30 2022 Because pragma(msg) is an inglorious hack. We need proper I/O at
- H. S. Teoh (7/14) Mar 30 2022 And pragma(msg) is technically not your program outputting the message,
- Max Samukha (17/22) Mar 30 2022 template add(int x)
- H. S. Teoh (11/33) Mar 30 2022 [...]
- Max Samukha (2/8) Apr 05 2022 That's the way to think about it. Thanks! )
- Tejas (46/71) Mar 30 2022 It's a normal template... what are you talking about??
- Max Samukha (3/4) Apr 05 2022 Or something like this
- Anonymous (7/35) Mar 28 2022 ##### I'll try another
- Petar Kirov [ZombineDev] (5/33) Mar 29 2022 ```d
- Max Samukha (3/7) Mar 29 2022 Where's the import importing imported? )
- Timon Gehr (3/14) Mar 29 2022 https://github.com/dlang/dmd/blob/master/src/dmd/dmodule.d#L1139
- Max Samukha (2/7) Mar 29 2022 Nice! I didn't know it'd been added to object.d.
- jmh530 (5/7) Mar 29 2022 Same.
- Petar Kirov [ZombineDev] (21/29) Mar 29 2022 `imported!"XXX".YYY` imports the symbol `YYY` from the module
- jmh530 (3/12) Mar 29 2022 Thanks!
- H. S. Teoh (58/62) Mar 29 2022 [...]
- jmh530 (13/14) Mar 29 2022 Interesting. Thanks.
- H. S. Teoh (12/22) Mar 29 2022 Using `with(imported!"std.stdio")`, printing "Hello, World!":
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (9/17) Apr 05 2022 Not efficient enough. :^)
**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
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 notIt 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
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
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
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
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 79Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
Mar 27 2022
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
On 3/28/22 06:33, Stanislav Blinov wrote:On Monday, 28 March 2022 at 05:23:22 UTC, FeepingCreature wrote: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 AliThrowing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```Report failure in case of success? :)
Mar 28 2022
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: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, K5Throwing 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
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
On Tuesday, 29 March 2022 at 07:06:57 UTC, Timon Gehr wrote:On 28.03.22 16:34, H. S. Teoh wrote:Or simply ```int main() => "Hello World".puts < 0;```import std.stdio; int main() => !(1 + "Hello World".puts);int main() => !~"Hello World".puts;
Mar 29 2022
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
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:No. Your variant loses error status and always reports success.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
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:Does it matter ๐ SDB 79On Tuesday, 29 March 2022 at 07:25:57 UTC, Max Samukha wrote:No. Your variant loses error status and always reports success.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
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: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? :DYour variant loses error status and always reports success.Does it matter ๐
Mar 29 2022
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:Why not ```pragma(msg, "Hello World");```On 28.03.22 16:34, H. S. Teoh wrote:Or simply ```int main() => "Hello World".puts < 0;```import std.stdio; int main() => !(1 + "Hello World".puts);int main() => !~"Hello World".puts;
Mar 30 2022
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
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: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.Why not ```pragma(msg, "Hello World");```Because pragma(msg) is an inglorious hack. We need proper I/O at compile time.
Mar 30 2022
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. ;-) Ttemplate 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
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
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 TThat's the way to think about it. Thanks! )
Apr 05 2022
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: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)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. ;-) Ttemplate 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
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
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:2 line version ```d import std.stdio; void main() { "Hello World".writeln; } // Or "puts" ```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 79Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
Mar 28 2022
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:```d void main() => imported!`std`.writeln("Hello World"); ``` :PThank 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 79Throwing my hat in the ring: ```d import std.stdio; int main() => "Hello World".puts; ```
Mar 29 2022
On Tuesday, 29 March 2022 at 09:36:20 UTC, Petar Kirov [ZombineDev] wrote:```d void main() => imported!`std`.writeln("Hello World"); ``` :PWhere's the import importing imported? )
Mar 29 2022
On 29.03.22 13:56, Max Samukha wrote:On Tuesday, 29 March 2022 at 09:36:20 UTC, Petar Kirov [ZombineDev] wrote:https://github.com/dlang/dmd/blob/master/src/dmd/dmodule.d#L1139 https://github.com/dlang/druntime/blob/master/src/object.d#L5087```d void main() => imported!`std`.writeln("Hello World"); ``` :PWhere's the import importing imported? )
Mar 29 2022
On Tuesday, 29 March 2022 at 12:52:17 UTC, Timon Gehr wrote:Nice! I didn't know it'd been added to object.d.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
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
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:`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[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
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
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
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
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
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