www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - T... args!

reply Salih Dincer <salihdb hotmail.com> writes:
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
parent reply Adam Ruppe <destructionator gmail.com> writes:
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
parent reply Salih Dincer <salihdb hotmail.com> writes:
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
next sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
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:
 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); } ```
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-templates
Dec 09 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/8/21 7:36 PM, Salih Dincer wrote:
 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); } ```
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. -Steve
Dec 09 2021
parent reply Salih Dincer <salihdb hotmail.com> writes:
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.

 -Steve
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? SDB 79
Apr 28 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Apr 29 2022
next sibling parent reply Tejas <notrealemail gmail.com> writes:
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?
Apr 29 2022
parent Dennis <dkorpel gmail.com> writes:
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
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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
parent reply Tejas <notrealemail gmail.com> writes:
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
Apr 29 2022
parent Salih Dincer <salihdb hotmail.com> writes:
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
Apr 29 2022