Although I agree that having a default constructor would be convenient, I think the problem is that S.init should be known at compile time by the spec.<div><br><div>Maybe we could support the following syntax, with a static this instead of this :</div>
<div><br></div><div>struct S<br>{<br> int x=11;<br> int y = void;<br><br> static this() // hypothetical<br> {<br> // x would already be initialized to int.init here<br> assert(x == int.init);<br>
// y is left uninitialized here</div><div> y=x*x;<br> }<br>}<br><div><br><div class="gmail_quote">On Fri, May 17, 2013 at 2:34 PM, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 5/17/13, Walter Bright <<a href="mailto:walter@digitalmars.com">walter@digitalmars.com</a>> wrote:<br>
> I oppose this. D has a lot of nice features because of the .init property.<br>
> Default constructors wreck that.<br>
<br>
Would they? I'm thinking the process would be:<br>
<br>
struct S<br>
{<br>
int x;<br>
int y = void;<br>
<br>
this() // hypothetical<br>
{<br>
// x would already be initialized to int.init here<br>
assert(x == int.init);<br>
<br>
// y is left uninitialized here<br>
}<br>
}<br>
<br>
Maybe that's already clear. But why is .init actually such a big<br>
problem? If it becomes arbitrarily expensive to call .init of a<br>
struct, well it's because it has to be - if the user really provided<br>
an expensive default ctor. But it's entirely the user's<br>
responsibility. So then .init can even throw, but throwing exceptions<br>
isn't a big deal. Is there some other problem?<br>
<br>
A custom default ctor in a struct is one of the most asked for<br>
features. Just yesterday we spent several hours explaining to a C++<br>
user why a default ctor doesn't work, and what .init is for. The whole<br>
conversation could have been avoided if D had support for custom<br>
default ctors for structs. This topic comes up very often in IRC and<br>
the forums.<br>
</blockquote></div><br></div></div></div>