www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template property

reply Zarathustra <adam.chrapkowski gmail.com> writes:
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)(       ); 
}
Aug 24 2008
parent Christopher Wright <dhasenan gmail.com> writes:
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.
Aug 24 2008