Thread overview
[Issue 3438] New: constructor with defaulted parameters ignored
Nov 09, 2009
Eldar Insafutdinov
Nov 11, 2009
Eldar Insafutdinov
Jan 23, 2012
Jonathan M Davis
[Issue 3438] struct ctor with defaulted parameters should be rejected
Dec 21, 2012
Andrej Mitrovic
Sep 17, 2013
Andrej Mitrovic
October 23, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3438

           Summary: constructor with defaulted parameters ignored
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei@metalanguage.com> 2009-10-23 07:44:31 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3438


Eldar Insafutdinov <e.insafutdinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |e.insafutdinov@gmail.com


--- Comment #1 from Eldar Insafutdinov <e.insafutdinov@gmail.com> 2009-11-09 13:18:26 PST ---
I would also like to have default struct constructors implemented. So I give a vote to this one.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 11, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3438



--- Comment #2 from Eldar Insafutdinov <e.insafutdinov@gmail.com> 2009-11-10 22:39:12 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 23, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3438


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #3 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-01-22 16:16:31 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3438


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |accepts-invalid, pull
                 CC|                            |andrej.mitrovich@gmail.com
           Platform|Other                       |All
            Version|2.000                       |D2
         AssignedTo|nobody@puremagic.com        |andrej.mitrovich@gmail.com
            Summary|constructor with defaulted  |struct ctor with defaulted
                   |parameters ignored          |parameters should be
                   |                            |rejected
         OS/Version|Linux                       |All


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-21 15:52:48 PST ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3438



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-17 12:15:54 PDT ---
*** Issue 10952 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------