Bug 3438 – struct ctor with defaulted parameters should be rejected

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-10-23T07:44:00Z
Last change time
2016-01-03T14:02:04Z
Keywords
accepts-invalid, pull
Assigned to
andrej.mitrovich
Creator
andrei

Comments

Comment #0 by andrei — 2009-10-23T07:44:31Z
struct foo { this(int dummy = 0) { writeln("Default constructor");} } void main() { foo x = foo(); } does not print anything. The code should either not compile or print something. I think at some point we'll need to support default constructors that execute code.
Comment #1 by e.insafutdinov — 2009-11-09T13:18:26Z
I would also like to have default struct constructors implemented. So I give a vote to this one.
Comment #2 by e.insafutdinov — 2009-11-10T22:39:12Z
To add a point, by default constructor should be invoked if present. There may be some reasons to keep a way to create an uninitialized struct(yeah, D is a systems language), but it should be done explicitly. Now we have an opposite situation, where by default struct is not initialized, and if you want to, you can define static opCall like in D1(what an ugly hack!) and call it manually. I have the real word example for that as well. I am porting Qt container classes to D. They are value types with Copy on Write semantics and atomic reference counting. They need some initialization (reference increment) when constructed. So basically if I write something like this: QList!int a; It would not be a valid code. So I have to do auto a = QList!int(); That is annoying, unsafe(I would have write something in the docs: always initialize with static opCall!) and not consistent with other value types.
Comment #3 by issues.dlang — 2012-01-22T16:16:31Z
At this point, I don't think that the situation with default constructors and structs is going to change. It's a result of requiring init properties for all types, and is thus a "forced fault" in the language. From discussions on it in the newsgroup, it seems likely that if we were to create a restricted default constructor (one which didn't do the things that we can't let it do, because it woludn't work with init), it would too restrictive to really be of much use anyway. So, I think that all of that strays from the point of this bug. Having a default argument for all of the parameters of a struct's constructor should result in a compilation error. It's illegal. The language doesn't support it. But rather than giving an error, the compiler currently just ignores it.
Comment #4 by andrej.mitrovich — 2012-12-21T15:52:48Z
Changed what was basically an enhancement request into an accepts-invalid bug. If the state of things change we can work on it later (if Walter green-lights default ctors for structs), but for now all forms of default ctors in structs must be rejected by the compiler. https://github.com/D-Programming-Language/dmd/pull/1397
Comment #5 by andrej.mitrovich — 2013-09-17T12:15:54Z
*** Issue 10952 has been marked as a duplicate of this issue. ***
Comment #6 by andrej.mitrovich — 2014-03-04T02:59:36Z
*** Issue 12295 has been marked as a duplicate of this issue. ***
Comment #7 by github-bugzilla — 2015-10-16T13:24:04Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/582dae0699d8b29d424b5fd15ca8b98ec5674efc Fix Issue 3438 - Struct ctor with all defaulted parameters should be rejected https://github.com/D-Programming-Language/dmd/commit/21bb8efff4e9cd05b160475120088a1f36dfc798 Merge pull request #5185 from AndrejMitrovic/fix-3438 Issue 3438 - Struct ctor with all defaulted parameters should be rejected
Comment #8 by github-bugzilla — 2016-01-03T14:02:04Z
Commits pushed to stable at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/582dae0699d8b29d424b5fd15ca8b98ec5674efc Fix Issue 3438 - Struct ctor with all defaulted parameters should be rejected https://github.com/D-Programming-Language/dmd/commit/21bb8efff4e9cd05b160475120088a1f36dfc798 Merge pull request #5185 from AndrejMitrovic/fix-3438