Re: T... args!
On Saturday, 30 April 2022 at 02:22:22 UTC, Tejas wrote: On Friday, 29 April 2022 at 16:10:52 UTC, Salih Dincer wrote: On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote: [...] I see, think it can be done with mixin: ```d template prn(alias args) { string prn() { string result = "write("; foreach(s; args.split("|")) { result ~= format("%s,", s); } return result ~ ");"; } } void main() { int data = 456; char enter = '\n'; mixin(prn!q{ enter| 123 | " str " | data | enter|__TIME__ | enter } ); mixin( prn!q{"This value of " |41| " is prime."} ); } ``` If there was some convenience on the compiler side, we could integrate it into D. SDB@79 There's already an implementation of `mixin` string interpolation in https://github.com/Abscissa/scriptlike I took a quick look and didn't like it! Maybe it would be more useful to use it in a comprehensive project. I like simple things more. In D, I would like to be able to write: ```d printq("Value: " stack.pop ", address 0x" stack.length + stack.ptr); ``` But due to a habit from C, I say like this: ```d writef("Value: %s, address 0x%s", stack.pop, stack.length + stack.ptr); ``` Longer, yes, but I respect it. There are many people like me who love the old. But I also love the new conveniences: ```d with(new Stack!IDs) { while(!empty) { "Value".write(": ", pop); ", ".writefln!"%saddress 0x%x" (length + ptr); } } ``` I like these too! But it's not enough! It's not enough, you know? I'm a human! I don't know what postfix is! I have to keep up with the computer not it me. The computer has to keep up with me, not me! Because I created it. Ve's-selam, SDB@79
Re: T... args!
On Friday, 29 April 2022 at 16:10:52 UTC, Salih Dincer wrote: On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote: [...] I see, think it can be done with mixin: ```d template prn(alias args) { string prn() { string result = "write("; foreach(s; args.split("|")) { result ~= format("%s,", s); } return result ~ ");"; } } void main() { int data = 456; char enter = '\n'; mixin(prn!q{ enter| 123 | " str " | data | enter|__TIME__ | enter } ); mixin( prn!q{"This value of " |41| " is prime."} ); } ``` If there was some convenience on the compiler side, we could integrate it into D. SDB@79 There's already an implementation of `mixin` string interpolation in https://github.com/Abscissa/scriptlike
Re: T... args!
On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote: There is no string interpolation in D. You can use a function such as `std.conv.text` to produce a string given interleaving strings and items. Or you can use `std.format.format` to make it happen. I see, think it can be done with mixin: ```d template prn(alias args) { string prn() { string result = "write("; foreach(s; args.split("|")) { result ~= format("%s,", s); } return result ~ ");"; } } void main() { int data = 456; char enter = '\n'; mixin(prn!q{ enter| 123 | " str " | data | enter|__TIME__ | enter } ); mixin( prn!q{"This value of " |41| " is prime."} ); } ``` If there was some convenience on the compiler side, we could integrate it into D. SDB@79
Re: T... args!
On Friday, 29 April 2022 at 15:13:08 UTC, Tejas wrote: It's not a keyword yet it's recognised specially by the compiler... What? It's not really recognized by the compiler, there's a little bit of magic to print `string` in outputted D code (e.g. error messages) instead of `immutable(char)[]`, but that's it.
Re: T... args!
On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote: On 4/28/22 10:48 PM, Salih Dincer wrote: [...] There is no string interpolation in D. You can use a function such as `std.conv.text` to produce a string given interleaving strings and items. Or you can use `std.format.format` to make it happen. It looks like you want to take any number of parameters as a template parameter comprised of a tuple of strings? That isn't supported. What you instead created was a tuple with a name that shadows the outer name. Note that `string` is NOT a keyword in D, it's just an alias (one that the compiler recognizes specially). So it's possible to redefine string as a symbol to mean something else. -Steve It's not a keyword yet it's recognised specially by the compiler... What? I'm understanding the concept over here, but why didn't we make `string` a keyword anyways since the compiler recognises it specially?
Re: T... args!
On 4/28/22 10:48 PM, Salih Dincer wrote: On Thursday, 9 December 2021 at 14:34:58 UTC, Steven Schveighoffer wrote: You may want to post what you want to achieve with your code, instead of examples of what you tried, and it may allow us to make things clearer. You are likely using the wrong construct to achieve your goals. Hi, sorry to bring up an old topic again, but I'm not a zombie. It's true I'm a dinosaur. What I want to do is: What is supported in many programming languages but not in modern D programming language: String Interpolation I believe this will be done using the template. What is this D template called as the special syntax called? There is no string interpolation in D. You can use a function such as `std.conv.text` to produce a string given interleaving strings and items. Or you can use `std.format.format` to make it happen. It looks like you want to take any number of parameters as a template parameter comprised of a tuple of strings? That isn't supported. What you instead created was a tuple with a name that shadows the outer name. Note that `string` is NOT a keyword in D, it's just an alias (one that the compiler recognizes specially). So it's possible to redefine string as a symbol to mean something else. -Steve
Re: Is T.init allowed?
On Friday, 29 April 2022 at 12:05:35 UTC, Dennis wrote: On Friday, 29 April 2022 at 11:30:49 UTC, Andrey Zherikov wrote: Is it a compiler issue so this shouldn't be allowed? Members called `init` are in the process of being deprecated, see: https://github.com/dlang/dmd/pull/12512 That's nice!
Re: Is T.init allowed?
On Friday, 29 April 2022 at 11:30:49 UTC, Andrey Zherikov wrote: Is it a compiler issue so this shouldn't be allowed? Members called `init` are in the process of being deprecated, see: https://github.com/dlang/dmd/pull/12512
Is T.init allowed?
Seems compiler allows custom nested type with `init` name: ```d struct T { struct init {} int b = 2; } void main() { T t; writeln(t);// T(2) //writeln(T.init); // Error: cannot pass type `init` as a function argument } ``` Is it a compiler issue so this shouldn't be allowed? If this is OK then how can I refer to an initial value of `T` (`T.init` doesn't work)?