return type and templates

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 22 06:17:08 PST 2013


On Friday, November 22, 2013 14:29:46 Timon Gehr wrote:
> On 11/22/2013 01:29 PM, Jonathan M Davis wrote:
> > On Friday, November 22, 2013 11:50:57 Andrea Fontana wrote:
> >> I just mean:
> >> 
> >> int t = s.value;  // Means  int t = s.value!int;
> >> 
> >> If there's a problem with template instantiatio is the same we
> >> have now.
> >> Now I have to write:
> >> 
> >> int t = s.value!int;
> >> 
> >> so if there's a problem with !int, it's just like now.
> >> 
> >> It's just a syntactic sugar, no new feature... Am I wrong?
> > 
> > Again, how is the compiler supposed to have any clue that you want to
> > instantiate value with int in the case of
> > 
> > int t = s.value;
> > 
> > The left-hand side of the expression has no impact on the type of the
> > right- hand side,
> 
> If you mean the type of the variable declaration, then yes it does.
> 
> int delegate(int) dg1 = x=>x;
> float delegate(float) dg2 = x=>x;
> 
> static assert(!is(typeof(x=>x)));

There are a few cases where the compiler does that but not many. In general, 
the right-hand side of an assignment is evaluated separately from the left and 
thus gets no type information from the left-hand side. But even if it did, in 
this case, that would mean determining the template argument from the return 
type, which is completely backwards to how template instantiation works, and 
attempting that would be a lot like attempting to overload on the return type 
of a function, which completely goes against how C-based languages work.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list