T... args!
Steven Schveighoffer
schveiguy at gmail.com
Thu Dec 9 14:34:58 UTC 2021
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
More information about the Digitalmars-d-learn
mailing list