Thread overview |
---|
January 15, 2008 What about this... | ||||
---|---|---|---|---|
| ||||
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) |
January 15, 2008 Re: What about this... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | 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
|
January 15, 2008 Re: What about this... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | 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
|
January 15, 2008 Re: What about this... | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | 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)
|
January 15, 2008 Re: What about this... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | 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;}
}
|
Copyright © 1999-2021 by the D Language Foundation