template property

Christopher Wright dhasenan at gmail.com
Sun Aug 24 06:02:12 PDT 2008


Zarathustra wrote:
> Is it possible to create template property without set and get prefixes?
> 
> public TResult 
> value/* setValue */(TResult = char[])(TResult o_value){ 
>   return this.SetValue!(TResult)(o_value); 
> }
> 
> public TResult 
> value/* getValue */(TResult = char[])(               ){ 
>   return this.GetValue!(TResult)(       ); 
> }

You're overloading functions inside different templates. That isn't allowed.

If you can come up with a guard value for each type you use, you could do:

TResult value (TResult = char[]) (TResult o_value = GuardValue!(TResult))
{
	if (o_value !is GuardValue!(TResult))
	{
		return this.SetValue!(TResult)(o_value);
	}
	else
	{
		return this.GetValue!(TResult)();
	}
}

Or you could use varargs:
TResult value (TResult = char[]) (TResult[] o_values...)
{
	if (o_values.length)
	{
		return this.SetValue!(TResult)(o_values[0]);
	}
	else
	{
		return this.GetValue!(TResult)();
	}
}
auto value1 = obj.value!(Object);
obj.value = 1; // don't know whether IFTI works with this syntax

Or you could put the overloads inside the same template, but then you'd 
have to call it as:
auto value = obj.value!(type).value;

I'd probably use varargs, if it works.


More information about the Digitalmars-d-learn mailing list