www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ambiguous template parameter names

reply jmh530 <john.michael.hall gmail.com> writes:
In the function below, there is a template parameter and a normal 
parameter both with the same name. However, the function returns 
the normal parameter. The template parameter is effectively 
ignored. I was surprised by this behavior.

Is this a bug or intentional? I did not see it documented 
anywhere.


```
int foo(int val)(int val)
{
	return val;
}

void main()
{
     assert(foo!1(2) == 2);
}
```
May 02 2018
next sibling parent user1234 <user1234 12.nl> writes:
On Wednesday, 2 May 2018 at 20:32:43 UTC, jmh530 wrote:
 In the function below, there is a template parameter and a 
 normal parameter both with the same name. However, the function 
 returns the normal parameter. The template parameter is 
 effectively ignored. I was surprised by this behavior.

 Is this a bug or intentional? I did not see it documented 
 anywhere.


 ```
 int foo(int val)(int val)
 {
 	return val;
 }

 void main()
 {
     assert(foo!1(2) == 2);
 }
 ```
i think this is a bug and that it should be reported. The main problem is that you cant even use the val (the template parameter one) in a static if.
May 02 2018
prev sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 2 May 2018 at 20:32:43 UTC, jmh530 wrote:
 In the function below, there is a template parameter and a 
 normal parameter both with the same name. However, the function 
 returns the normal parameter. The template parameter is 
 effectively ignored. I was surprised by this behavior.

 Is this a bug or intentional? I did not see it documented 
 anywhere.


 ```
 int foo(int val)(int val)
 {
 	return val;
 }

 void main()
 {
     assert(foo!1(2) == 2);
 }
 ```
It's not a big per se. It's a consequence of the declaration expanding to the real template function form (I can't type it all out as I'm on my phone), thus the inner `val` from the function shadows the one from the template.
May 02 2018
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 3 May 2018 at 00:52:58 UTC, Meta wrote:
 [snip]

 It's not a big per se. It's a consequence of the declaration 
 expanding to the real template function form (I can't type it 
 all out as I'm on my phone), thus the inner `val` from the 
 function shadows the one from the template.
That makes sense. Before creating the example, I would have assumed that when you instantiate it as foo!1, then the `val=1` would flow through to the inner foo function. template foo(int val) { int foo(int val) { return val; } }
May 02 2018
parent reply Meta <jared771 gmail.com> writes:
On Thursday, 3 May 2018 at 02:48:10 UTC, jmh530 wrote:
 On Thursday, 3 May 2018 at 00:52:58 UTC, Meta wrote:
 [snip]

 It's not a big per se. It's a consequence of the declaration 
 expanding to the real template function form (I can't type it 
 all out as I'm on my phone), thus the inner `val` from the 
 function shadows the one from the template.
That makes sense. Before creating the example, I would have assumed that when you instantiate it as foo!1, then the `val=1` would flow through to the inner foo function. template foo(int val) { int foo(int val) { return val; } }
If you want that, you might be able to do `int val = val` on the inner function, though I'm not sure that'll work.
May 02 2018
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 3 May 2018 at 02:51:18 UTC, Meta wrote:
 If you want that, you might be able to do `int val = val` on 
 the inner function, though I'm not sure that'll work.
It does not work and will do nothing.
May 03 2018
parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 3 May 2018 at 13:30:03 UTC, bauss wrote:
 On Thursday, 3 May 2018 at 02:51:18 UTC, Meta wrote:
 If you want that, you might be able to do `int val = val` on 
 the inner function, though I'm not sure that'll work.
It does not work and will do nothing.
Below compiles, but my guess is that the ASM is saying that the foo function only has a run-time argument. template foo(int val) { int foo(int val = val) { return val; } } void main() { assert(foo!1 == 1); assert(foo!1(2) == 2); }
May 04 2018