Thread overview | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 09, 2014 Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Just to be clear, I don't want a default constructor for structs that gets called implictly by the compiler, like in C++. Instead I would really love to have a explicit default constructor. E.g. it could look like this (alternative a new keyword "explicit" could be introduced, but introduction of new keywords is usually avoided if possible, AFAIK): struct Foo { this(void) { // do stuff here } } This default constructor would _never_ be called automatically by the compiler. (e.g. when a class is constructed that has struct members.) It would only be called in cases where the user explictly calls it. The following lines would call the explicit default constructor auto foo1 = Foo(); auto foo2 = new Foo(); foo1 = Foo(); // calls explicit constructor first, then calls assignment operator Whereas the follwing would _not_ call the explicit default constructor. class Bar { Foo m_foo; } auto bar = new Bar(); Foo foo; // does not call the explict default constructor, because there is no explicit call here I think this would fix all cases where you currently wish for a struct default constructor in D. Coupeled with "@disable this();" you could force users to always call one of the struct constructors. Currently I work around the issue of not having any default constructors by doing this: struct DefaultCtor {}; //call default ctor type enum defaultCtor = DefaultCtor(); struct Foo() { @disable this(); this(DefaultCtor) { // default constructor } this(int) { // other constructor } } auto foo = Foo(defaultCtor); While this works, I'm getting anoyed by it every day. For example when refactoring types from Classes to Structs and vise versa. As well as when placing Classes on the stack using a helper struct. Or when having RAII structs that don't take any paramters in their constructor. What do you think? C&C welcome. Kind Regards Benjamin Thaut |
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Wednesday, 9 April 2014 at 14:59:35 UTC, Benjamin Thaut wrote:
> Just to be clear, I don't want a default constructor for structs that gets called implictly by the compiler, like in C++.
>
> Instead I would really love to have a explicit default constructor. E.g. it could look like this (alternative a new keyword "explicit" could be introduced, but introduction of new keywords is usually avoided if possible, AFAIK):
>
> struct Foo
> {
> this(void)
> {
> // do stuff here
> }
> }
>
> This default constructor would _never_ be called automatically by the compiler. (e.g. when a class is constructed that has struct members.) It would only be called in cases where the user explictly calls it.
>
> The following lines would call the explicit default constructor
>
> auto foo1 = Foo();
> auto foo2 = new Foo();
> foo1 = Foo(); // calls explicit constructor first, then calls assignment operator
>
> Whereas the follwing would _not_ call the explicit default constructor.
>
> class Bar
> {
> Foo m_foo;
> }
> auto bar = new Bar();
>
> Foo foo; // does not call the explict default constructor, because there is no explicit call here
>
> I think this would fix all cases where you currently wish for a struct default constructor in D. Coupeled with "@disable this();" you could force users to always call one of the struct constructors. Currently I work around the issue of not having any default constructors by doing this:
>
> struct DefaultCtor {}; //call default ctor type
>
> enum defaultCtor = DefaultCtor();
>
> struct Foo()
> {
> @disable this();
> this(DefaultCtor)
> {
> // default constructor
> }
>
> this(int)
> {
> // other constructor
> }
> }
>
> auto foo = Foo(defaultCtor);
>
> While this works, I'm getting anoyed by it every day. For example when refactoring types from Classes to Structs and vise versa. As well as when placing Classes on the stack using a helper struct. Or when having RAII structs that don't take any paramters in their constructor.
>
> What do you think? C&C welcome.
>
> Kind Regards
> Benjamin Thaut
This would be very nice. Yes please.
|
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On Wednesday, 9 April 2014 at 14:59:35 UTC, Benjamin Thaut wrote:
> What do you think? C&C welcome.
>
> Kind Regards
> Benjamin Thaut
I haven't read though the entire proposal yet (sorry!), but I'm in definite agreement that *something* needs to be done to allow explicit but argument-less construction of structs.
|
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | What would this do? struct SomeStruct { this(int i = 10) { this.i = i; } this(void) { this.i = 20; } int i; } auto s = SomeStruct(); |
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On 2014-04-09 16:59, Benjamin Thaut wrote: > Just to be clear, I don't want a default constructor for structs that > gets called implictly by the compiler, like in C++. > > Instead I would really love to have a explicit default constructor. E.g. > it could look like this (alternative a new keyword "explicit" could be > introduced, but introduction of new keywords is usually avoided if > possible, AFAIK): > > struct Foo > { > this(void) > { > // do stuff here > } > } > > This default constructor would _never_ be called automatically by the > compiler. (e.g. when a class is constructed that has struct members.) It > would only be called in cases where the user explictly calls it. > > The following lines would call the explicit default constructor > > auto foo1 = Foo(); > auto foo2 = new Foo(); > foo1 = Foo(); // calls explicit constructor first, then calls assignment > operator What's the advantage over using a static opCall, that it works with "new"? -- /Jacob Carlborg |
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian Schott | On 2014-04-09 18:59, Brian Schott wrote: > What would this do? > > struct SomeStruct > { > this(int i = 10) > { > this.i = i; > } > > this(void) > { > this.i = 20; > } > > int i; > } > > auto s = SomeStruct(); Result in an ambiguity error? -- /Jacob Carlborg |
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 9 April 2014 at 17:07:13 UTC, Jacob Carlborg wrote:
> Result in an ambiguity error?
Really? What does this program print using a current version of DMD?
import std.stdio;
struct SomeStruct
{
this(int i = 10)
{
this.i = i;
}
int i;
}
void main(string[] args)
{
auto s = SomeStruct();
writeln("s.i = ", s.i);
}
I don't think it would be ambiguous at all :-)
|
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On 04/09/2014 04:59 PM, Benjamin Thaut wrote:
>
> Instead I would really love to have a explicit default constructor. E.g.
> it could look like this (alternative a new keyword "explicit" could be
> introduced, but introduction of new keywords is usually avoided if
> possible, AFAIK):
>
> struct Foo
> {
> this(void)
> {
> // do stuff here
> }
> }
Why not just:
struct Foo{
this(){
// do stuff here
}
}
void main(){
Foo foo1; // error, no init value
auto foo2=Foo(); // ok
}
|
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benjamin Thaut | On 4/9/2014 7:59 AM, Benjamin Thaut wrote:
> What do you think? C&C welcome.
Or you could use a factory function:
struct Foo {
static Foo factory() { ... }
...
}
auto foo = Foo.factory();
|
April 09, 2014 Re: Explicit default constructor for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brian Schott | Am 09.04.2014 18:59, schrieb Brian Schott:
> What would this do?
>
> struct SomeStruct
> {
> this(int i = 10)
> {
> this.i = i;
> }
>
> this(void)
> {
> this.i = 20;
> }
>
> int i;
> }
>
> auto s = SomeStruct();
Thats easy to answer. What would it do if you replace the "struct" with "class" and the "void" with nothing?
|
Copyright © 1999-2021 by the D Language Foundation