What's the rationale here? alias this and function arguments

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 03:45:15 PDT 2015


On Tuesday, 10 March 2015 at 10:27:14 UTC, John Colvin wrote:
> struct S
> {
> 	int a;
> 	this(T)(T v)
> 	{
> 		this = v;
> 	}
> 	void foo(T)(T v)
> 	{
> 		import std.conv : to;
> 		a = v.to!int;
> 	}
> 	alias foo this;
> }
>
> void bar(S s){}
>
> void main()
> {
> 	S s0;
> 	s0 = "3"; //OK
> 	S s = "3"; //OK
> 	bar("3"); //Not OK
> }
>
> It would seem logical that the last one would work as well. 
> What's the reasoning behind this?

Do you think that the parameter should be automatically created 
from the argument ? Is this kind of thing even possible in 
another context ?

Your alias looks more like an ```opCall()```. To my eyes the 
strange thing is that if you add the following statement at the 
end of you sample:
---
s(0);
---

DMD outputs:

```Error: cannot resolve type for s.foo(T)(T v)```


More information about the Digitalmars-d-learn mailing list