digitalmars.D.learn - T... args!
- Salih Dincer (15/15) Dec 08 2021 Hi all,
- Adam Ruppe (8/11) Dec 08 2021 `string...` there is a user-defined identifier representing a mix
- Salih Dincer (12/16) Dec 08 2021 Not exactly...
- Petar Kirov [ZombineDev] (72/90) Dec 09 2021 Unlike [value template parameters][0] (which constitute of
- Steven Schveighoffer (27/48) Dec 09 2021 So what people are saying here (but not directly) is that `str` inside
- Salih Dincer (10/15) Apr 28 2022 Hi, sorry to bring up an old topic again, but I'm not a zombie.
- Steven Schveighoffer (11/25) Apr 29 2022 There is no string interpolation in D. You can use a function such as
- Tejas (7/21) Apr 29 2022 It's not a keyword yet it's recognised specially by the
- Dennis (4/6) Apr 29 2022 It's not really recognized by the compiler, there's a little bit
- Salih Dincer (32/36) Apr 29 2022 I see, think it can be done with mixin:
- Tejas (3/36) Apr 29 2022 There's already an implementation of `mixin` string interpolation
- Salih Dincer (32/75) Apr 29 2022 I took a quick look and didn't like it! Maybe it would be more
Hi all, Is this not a contradiction? : and 3.1415 aren't string: ```d void foo(string...)(string args) { foreach(ref str; args) { str.writeln('\t', typeof(str).stringof); } } foo("Pi", "number", ':', 3.1415); /* Pi string number string : char 3,1415 double //*/ ```
Dec 08 2021
On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote:Is this not a contradiction? : and 3.1415 aren't string: ```d void foo(string...)(string args) {`string...` there is a user-defined identifier representing a mix of types. string isn't special, yo can declare your own variables with that name. And that's what you did here. I think you meant to say void foo(string[] args...) {} which is avariadtic number of only strings
Dec 08 2021
On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:On Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote: I think you meant to say void foo(string[] args...) {}Not exactly... ```d alias str = immutable(char)[]; void foo(str...)(str args) { foreach(ref a; args) { a.writeln('\t', typeof(a).stringof); } str s; // "Amazing! ---v"; s.writeln(": ", typeof(s).stringof); } ```
Dec 08 2021
On Thursday, 9 December 2021 at 00:36:29 UTC, Salih Dincer wrote:On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:Unlike [value template parameters][0] (which constitute of existing type + identifier), all other template parameter forms introduce a brand new identifier in the template scope that is completely unrelated to whatever other types you may have outside in your program (including the ones implicitly imported from `object.d` like `string`). The `str...` in your `foo` function introduces a [template sequence parameter][1] which shadows the `str` `alias` you have above. The `str s;` line declares a variable of the `str` type sequence, so it's essentially a tuple (*). See: See: ```d import std.stdio : writeln; alias str = immutable(char)[]; void foo(str...)(str args) { foreach(ref a; args) { a.writeln('\t', typeof(a).stringof); } str s; // "Amazing! ---v"; s.writeln(": ", typeof(s).stringof); } void main() { foo(1, true, 3.5); } ``` ``` 1 int true bool 3.5 double 0falsenan: (int, bool, double) ``` (*) Technically, you can attempt to explicitly instantiate `foo` with non type template arguments, but it will fail to compile, since: * The `args` function parameter demands `str` to be a type (or type sequence) * The `s` function local variable demands `str` to be a type (or type sequence) If you can either remove `args` and `s` or filter the sequence to keep only the types: ```d import std.meta : Filter; enum bool isType(alias x) = is(x); alias TypesOnly(args...) = Filter!(isType, args); void foo(str...)(TypesOnly!str args) { static foreach(s; str) pragma (msg, s); } void main() { static immutable int a = 42; foo!(int, double, string)(3, 4.5, "asd"); pragma (msg, `----`); foo!(a, "asd", bool, foo, int[])(true, []); } ``` ``` int double string ---- 42 asd bool foo(str...)(TypesOnly!str args) int[] ``` [0]: https://dlang.org/spec/template.html#template_value_parameter [1]: https://dlang.org/spec/template.html#variadic-templatesOn Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote: I think you meant to say void foo(string[] args...) {}Not exactly... ```d alias str = immutable(char)[]; void foo(str...)(str args) { foreach(ref a; args) { a.writeln('\t', typeof(a).stringof); } str s; // "Amazing! ---v"; s.writeln(": ", typeof(s).stringof); } ```
Dec 09 2021
On 12/8/21 7:36 PM, Salih Dincer wrote:On Wednesday, 8 December 2021 at 23:47:07 UTC, Adam Ruppe wrote:So what people are saying here (but not directly) is that `str` inside your template is a *different* symbol than `str` outside the template. This code is *exactly the same* as the code above, it might help understand what is happening: ```d alias foobar = immutable(char)[]; // this doesn't play into the template at all void foo(str...)(str args) { foreach(ref a; args) { a.writeln('\t', typeof(a).stringof); } str s; // "Amazing! ---v"; s.writeln(": ", typeof(s).stringof); } ``` Just like the two `i` below refer to different things: ```d int i; void foo(int i) { writeln(i); // this is the function parameter i, not the module-level i } ``` 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. -SteveOn Wednesday, 8 December 2021 at 23:43:48 UTC, Salih Dincer wrote: I think you meant to say void foo(string[] args...) {}Not exactly... ```d alias str = immutable(char)[]; void foo(str...)(str args) { foreach(ref a; args) { a.writeln('\t', typeof(a).stringof); } str s; // "Amazing! ---v"; s.writeln(": ", typeof(s).stringof); } ```
Dec 09 2021
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. -SteveHi, 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? SDB 79
Apr 28 2022
On 4/28/22 10:48 PM, Salih Dincer wrote:On Thursday, 9 December 2021 at 14:34:58 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. 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. -SteveYou 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?
Apr 29 2022
On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote:On 4/28/22 10:48 PM, Salih Dincer wrote: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?[...]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
Apr 29 2022
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.
Apr 29 2022
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
Apr 29 2022
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:There's already an implementation of `mixin` string interpolation in https://github.com/Abscissa/scriptlike[...]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
Apr 29 2022
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: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 79On Friday, 29 April 2022 at 12:57:15 UTC, Steven Schveighoffer wrote:There's already an implementation of `mixin` string interpolation in https://github.com/Abscissa/scriptlike[...]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
Apr 29 2022