Bug 9977 – Function local templates should be allowed

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-22T01:35:00Z
Last change time
2013-04-22T10:57:37Z
Keywords
pull
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2013-04-22T01:35:50Z
Currently DMD accepts declaring template aggregates and functions inside functions. void test1() { struct S1(T) { T value; } auto func1(T)(T value) { return value; } static assert(is(S1!int == struct)); assert(func1(10) == 10); } But raw template declaration is not allowed. void test2() { template S2(T) { struct S2 { T value; } } // line 10 template func2(T) { auto func2(T value) { return value; } } // line 11 static assert(is(S2!int == struct)); assert(func2(10) == 10); } Output: test.d(10): Error: template definitions aren't allowed inside functions test.d(11): Error: template definitions aren't allowed inside functions Today, I think this is not reasonable limitation.
Comment #1 by k.hara.pg — 2013-04-22T01:43:43Z
Comment #2 by andrej.mitrovich — 2013-04-22T03:02:03Z
(In reply to comment #0) > Today, I think this is not reasonable limitation. Yes, especially since this: auto func1(T)(T value) { return value; } really translates into this: template func1(T) { auto func1(T value) { return value; } } At least that's what I remember. Thanks for working on this.
Comment #3 by github-bugzilla — 2013-04-22T03:38:51Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/707415dec87694c30a569dd1b67c496a9de7f648 fix Issue 9977 - Function local templates should be allowed https://github.com/D-Programming-Language/dmd/commit/006accc47fd0fb0a8299c258f66fdb0c505d9f16 Merge pull request #1919 from 9rnsr/fix9977 Issue 9977 - Function local templates should be allowed
Comment #4 by bearophile_hugs — 2013-04-22T10:24:06Z
In the changelog this should be underlined as a significant improvement of D. It's useful for unittests, to not pollute the module scope with template names that are used only inside the unittest.
Comment #5 by andrej.mitrovich — 2013-04-22T10:57:37Z
(In reply to comment #4) > In the changelog this should be underlined as a significant improvement of D. > It's useful for unittests, to not pollute the module scope with template names > that are used only inside the unittest. Yes. I've added it to the pending pull request.