convert TypeIdentifer refering to TemplateDeclaration to TypeInstance
text/plain
642
Comments
Comment #0 by thomas-dloop — 2007-02-28T08:10:22Z
# class Bar(T = int){
# }
#
# void foo(T = int)(){
# }
#
# void main(){
# foo(); // success
# auto b = new Bar(); // fails
# }
test.d(9): class test.Bar(T = int) is used as a type
test.d(9): Error: new can only create structs, dynamic arrays or class objects,
not void's
Comment #1 by aldacron — 2007-03-01T09:25:20Z
[email protected] wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=1012
>
> Summary: type templates don't support default arguments
> Product: D
> Version: 1.007
> Platform: PC
> OS/Version: All
> Status: NEW
> Severity: normal
> Priority: P3
> Component: DMD
> AssignedTo: [email protected]
> ReportedBy: [email protected]
>
>
> # class Bar(T = int){
> # }
> #
> # void foo(T = int)(){
> # }
> #
> # void main(){
> # foo(); // success
> # auto b = new Bar(); // fails
> # }
>
> test.d(9): class test.Bar(T = int) is used as a type
> test.d(9): Error: new can only create structs, dynamic arrays or class objects,
> not void's
>
>
Are you allowed to leave out the !() in class template instantations?
I'd expect to have to write
auto b = new Bar!()();
or at least
auto b = new Bar!();
AIUI this isn't supposed to work. If we allowed such implicit template instantiation everywhere, it would create ambiguities (alias declarations come to mind). That's why, at the moment, it's explicitly allowed in some contexts where it's unambiguous, such as IFTI and (to some degree) mixins.
Comment #4 by r.sagitario — 2009-09-30T14:48:47Z
Created attachment 466
convert TypeIdentifer refering to TemplateDeclaration to TypeInstance
I don't expect any additional ambiguities, because the template types are in the same namespace as all other identifers anyway (apart from version and debug identifiers).
This patch against 2.032 converts a TypeIdentifier (as created by the parser) into a TypeInstance if name lookup revealed that it is actually a template.
Comment #5 by clugdbug — 2012-09-26T00:51:01Z
*** Issue 1328 has been marked as a duplicate of this issue. ***
I have severe misgivings about this.
C has had problems with implicitly taking the address of a function. D has had a number of problems with implicitly calling a function (the @property thing).
It's just ambiguous in many cases, and leads to trouble, trouble, trouble.
Comment #8 by issues.dlang — 2012-11-16T10:58:46Z
> I have severe misgivings about this.
I concur. It seems like it just begging for trouble when providing minimal gain. Not to mention that it would harm code readability in many cases, which will therefore harm code maintainability.
issue# 6082 might be reasonable to implement (I don't know), in which case the example here would compile, but that would be restricted specifically to constructors. Making it so that templates in general can be instantiated without !() if they don't need any arguments is a bad idea IHMO. And I think that this post on the newsgroup is a good example of why this is a bad idea:
http://forum.dlang.org/post/[email protected]
Comment #9 by jakobovrum — 2012-11-16T11:14:54Z
(In reply to comment #8)
> issue# 6082 might be reasonable to implement (I don't know),
It's not possible in the general case.
http://stackoverflow.com/questions/13151072/
It might still be an attractive feature to have for specific, common cases, but it would either have to be defined as a "compiler's best effort" thing, which hurts portability, or defined to only work for a select few specific cases.
The latter is what's currently done for IFTI, but the rules are intuitive, especially thanks to eponymous templates.
Comment #11 by issues.dlang — 2017-12-10T05:54:18Z
*** Issue 18028 has been marked as a duplicate of this issue. ***
Comment #12 by pro.mathias.lang — 2019-05-21T01:15:05Z
The current error message is a bit better:
> test.d(9): Error: template class test.Bar(T = int) is used as a type without instantiation; to instantiate it use Bar!(arguments)
Given that:
- This bug has been open for 12 years
- There is no consensus in the favor of this enhancement
- Walter Bright has spoken against it
It should clearly go through the DIP process to properly assess if there would be any corner cases, semantic or parsing wise.