Jump to page: 1 25  
Page
Thread overview
Explicit default constructor for structs
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
John Colvin
Apr 09, 2014
monarch_dodra
Apr 09, 2014
Brian Schott
Apr 09, 2014
Jacob Carlborg
Apr 09, 2014
Brian Schott
Apr 09, 2014
Jacob Carlborg
Apr 10, 2014
Andrej Mitrovic
Apr 10, 2014
Remo
Apr 10, 2014
Jacob Carlborg
Apr 10, 2014
monarch_dodra
Apr 11, 2014
Daniel Murphy
Apr 11, 2014
Jonathan M Davis
Apr 11, 2014
Remo
Apr 11, 2014
Jonathan M Davis
Apr 11, 2014
Dicebot
Apr 11, 2014
monarch_dodra
Apr 11, 2014
Jacob Carlborg
Apr 11, 2014
monarch_dodra
Apr 11, 2014
Daniel Murphy
Apr 12, 2014
Jonathan M Davis
Apr 11, 2014
Jacob Carlborg
Apr 11, 2014
monarch_dodra
Apr 12, 2014
Jacob Carlborg
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
Brian Schott
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
Brian Schott
Apr 09, 2014
Jacob Carlborg
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
Timon Gehr
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
Timon Gehr
Apr 09, 2014
Benjamin Thaut
Apr 09, 2014
Walter Bright
Apr 09, 2014
Benjamin Thaut
Apr 10, 2014
monarch_dodra
Apr 10, 2014
Atila Neves
Apr 27, 2014
deadalnix
Apr 27, 2014
ketmar
Apr 27, 2014
Temtaime
April 09, 2014
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
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
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
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
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
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
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
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
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
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?
« First   ‹ Prev
1 2 3 4 5