return type and templates
Timon Gehr
timon.gehr at gmx.ch
Fri Nov 22 05:29:46 PST 2013
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)));
> and you have not given the compiler any information as to what
> template argument should be given to value.
Well, technically, in this case there is only one choice.
> s.value(3) only works because
> you've given value a function argument from which the corresponding template
> argument can be inforred. With s.value, you've given no indication whatsoever
> as to what value should be instantiated with.
>
> If you want a default template argument, then give it one. e.g.
>
> @property auto value(T = int)() if (is(T == int)) { return _intValue; }
>
> But I don't know how you expect the compiler to have any clue what type value
> should be instantiated with when you haven't given it any template arguments
> and there are no function arguments to infer the template arguments from -
> especially when this what the compiler really sees
>
> template value(T)
> if(is(T == int))
> {
> @property auto value() { return _intValue; }
> }
>
> and it doesn't even look at the template constraint, let alone the contents of
> the template, until you attempt to instantiate the template. And it's not
> going to be able to try and instantiate the template without having any
> template arguments.
> ...
The request would be reasonable if 'value' was declared as follows though:
@property T value(T)() if (is(T == int)) { return _intValue; }
i.e. the fact that the template argument equals the type of the
resulting call can be read off directly from the signature. This is in
the same ballpark as the existing IFTI features.
More information about the Digitalmars-d-learn
mailing list