digitalmars.D.learn - function-local template specialization error
- canalpay (29/29) Jul 14 2010 void main()
 - bearophile (4/12) Jul 14 2010 In D2 you can't define templates inside functions.
 - canalpay (8/12) Jul 14 2010 Thanks, But this example works:
 - bearophile (5/14) Jul 14 2010 You are right. Beside allowing nested functions (this is well known), it...
 - Don (8/25) Jul 14 2010 The spec explicitly says:
 
void main()
{
    T input(T)()
    {}
 
    T input(T : string)()
    {}
}
Error message :
isimsiz.d(6): found 'input' when expecting ';' following statement
isimsiz.d(6): found ':' when expecting ')'
isimsiz.d(6): found 'string' when expecting ';' following statement
isimsiz.d(6): found ')' instead of statement
isimsiz.d(8): unrecognized declaration
 
Shouldn't that work?
And
void main()
{
    void input(T)()
    {}
 
    void input(T : string)()
    {}
}
Again, Shouldn't that work?
Error message:
isimsiz.d(6): Error: declaration input(T : string) is already defined
there is no problem when the templates are in the global namespace.
 Jul 14 2010
canalpay:
 void main()
 {
     T input(T)()
     {}
  
     T input(T : string)()
     {}
 }
In D2 you can't define templates inside functions.
Bye,
bearophile
 Jul 14 2010
bearophile Wrote:In D2 you can't define templates inside functions. Bye, bearophileThanks, But this example works: void main() { T input(T)() {} } Why ?
 Jul 14 2010
canalpay:
 Thanks, But this example works:
 void main()
 {
     T input(T)()
     {}
 
 }
 
  Why ?
You are right. Beside allowing nested functions (this is well known), it seems
D2 also accepts the definition (with template constraint too) and instantiation
of simple templates inside functions, I didn't know this.
But currently nested functions can't be overloaded. Probably for similar
reasons inside a function you can't define more than one template with the same
name. I presume this limit can be removed, so if you really need this you can
ask for an enhancement request and if you can add a patch too to increase a bit
the probability to see it fulfilled someday.
Bye,
bearophile
 Jul 14 2010
bearophile wrote:canalpay:The spec explicitly says: "Unlike module level declarations, declarations within function scope are processed in order. This means that two nested functions cannot mutually call each other: [snip] Future directions: This restriction may be removed." My guess is that it will become possible eventually.Thanks, But this example works: void main() { T input(T)() {} } Why ?You are right. Beside allowing nested functions (this is well known), it seems D2 also accepts the definition (with template constraint too) and instantiation of simple templates inside functions, I didn't know this. But currently nested functions can't be overloaded. Probably for similar reasons inside a function you can't define more than one template with the same name. I presume this limit can be removed, so if you really need this you can ask for an enhancement request and if you can add a patch too to increase a bit the probability to see it fulfilled someday. Bye, bearophile
 Jul 14 2010








 
 
 
 Don <nospam nospam.com>