www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What about this...

reply Aarti_pl <aarti interia.pl> writes:
Any comments on this? Probably error, but maybe someone wants to add 
something...

--------

void pattern(T)(T v) {}

T pattern(T)() {return T.init;}

void main() {
     pattern!(int)(5);
}

---------

Above code results in compile time error:

quicktest.d(6): template instance pattern!(int) matches more than one 
template declaration, pattern(T) and pattern(T)
quicktest.d(6): Error: template instance 'pattern!(int)' is not a variable
quicktest.d(6): Error: function expected before (), not pattern!(int) of 
type int

---------

As you see it is standard D setter/getter approach with templates.

What's more interesting, when we call pattern template function with 
IFTI it calls proper function:

pattern(5); //calls setter

but:
pattern; // is error

BTW: I hope that in DMD 2.0
writefln;
will be also allowed. Currently it doesn't work - you have to write 
writefln();

BR
Marcin Kuszczak
(aarti_pl)
Jan 15 2008
next sibling parent Bjoern <nanali nospam-wanadoo.fr> writes:
Aarti_pl schrieb:
 Any comments on this? Probably error, but maybe someone wants to add 
 something...
 
 --------
 
 void pattern(T)(T v) {}
 
 T pattern(T)() {return T.init;}
 
 void main() {
     pattern!(int)(5);
 }
 
 ---------
 
 Above code results in compile time error:
 
 quicktest.d(6): template instance pattern!(int) matches more than one 
 template declaration, pattern(T) and pattern(T)
 quicktest.d(6): Error: template instance 'pattern!(int)' is not a variable
 quicktest.d(6): Error: function expected before (), not pattern!(int) of 
 type int
 
 ---------
 
 As you see it is standard D setter/getter approach with templates.
 
 What's more interesting, when we call pattern template function with 
 IFTI it calls proper function:
 
 pattern(5); //calls setter
 
 but:
 pattern; // is error
 
 BTW: I hope that in DMD 2.0
 writefln;
 will be also allowed. Currently it doesn't work - you have to write 
 writefln();
 
 BR
 Marcin Kuszczak
 (aarti_pl)
Sorry untested and just an idea :( What happens when you use : T pattern(T)(out T t) {return t;} instead of T pattern(T)() {return T.init;} Bjoern
Jan 15 2008
prev sibling next sibling parent reply downs <default_357-line yahoo.de> writes:
Aarti_pl wrote:
 Any comments on this? Probably error, but maybe someone wants to add
 something...
 
 --------
 
 void pattern(T)(T v) {}
 
 T pattern(T)() {return T.init;}
 
 void main() {
     pattern!(int)(5);
 }
 
 ---------
There's a dirty, dirty but working work-around for this :) T pattern(T)() { return T.init; } void pattern(T, BOGUS=void)(T v) { } then just use IFTI for the second one. --downs
Jan 15 2008
parent Aarti_pl <aarti interia.pl> writes:
downs pisze:
 Aarti_pl wrote:
 Any comments on this? Probably error, but maybe someone wants to add
 something...

 --------

 void pattern(T)(T v) {}

 T pattern(T)() {return T.init;}

 void main() {
     pattern!(int)(5);
 }

 ---------
There's a dirty, dirty but working work-around for this :) T pattern(T)() { return T.init; } void pattern(T, BOGUS=void)(T v) { } then just use IFTI for the second one. --downs
Thanks! Nice to know it! BR Marcin Kuszczak (aarti_pl)
Jan 15 2008
prev sibling parent BCS <BCS pathlink.com> writes:
Aarti_pl wrote:
 Any comments on this? Probably error, but maybe someone wants to add 
 something...
 
 --------
 
 void pattern(T)(T v) {}
 
 T pattern(T)() {return T.init;}
 
 void main() {
     pattern!(int)(5);
 }
 
 ---------
 
 Above code results in compile time error:
 
 quicktest.d(6): template instance pattern!(int) matches more than one 
 template declaration, pattern(T) and pattern(T)
 quicktest.d(6): Error: template instance 'pattern!(int)' is not a variable
 quicktest.d(6): Error: function expected before (), not pattern!(int) of 
 type int
 
the issue is more noticeable in this form template pattern(T) { void pattern(T v) {} } template pattern(T) { T pattern() {return T.init;} } void main() { pattern!(int)(5); } DMD can't pick witch template to do. My thoughts on fixing this would be to let this work: template pattern(T) { void pattern(T v) {} T pattern() {return T.init;} }
Jan 15 2008