return type and templates

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 22 02:03:12 PST 2013


On Friday, November 22, 2013 10:24:38 Andrea Fontana wrote:
> I've seen many different topic about this, but they don't explain
> what's wrong with this "proposed" feature. Who can explain me why
> this can't be added to language? Does it broke something?
> 
> // Trivial example:
> 
> struct Test
> {
> 	@property
> 	auto value(T)() if (is(T == int)) { return _intValue; }
> 
> 	@property
> 	void value(T)(T val) if (is(T == int)) { _intValue = val; }
> 
> 	private int _intValue;
> }

> 	auto t4 = s.value; // Doesn't work (and i don't think it should)
> 	int t3 = s.value; // <--Doesn't work (can this feature be
> implemented?)

And how would it know what type value should be instantiated with. It only 
knows in the case of

s.value(7)

because you gave it a value from which the compiler was able to infer the 
type. You didn't tell it anything in the case of

s.value

And it's not like the compiler can look at the template constraint and guess 
what will work or not - especially when template constraints can be 
arbitrarily complex. As far as the compiler is concerned, it's just a boolean 
expression which determines whether a given template instantiation is valid or 
not. At most, it's used for overloading when there are multiple templates 
which would otherwise match the given arguments. It's not going to work for 
the compiler to figure out what types might work with a given template 
constraint and then have it pick one when you don't tell the template what 
type to be instantiated with.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list