www.digitalmars.com         C & C++   DMDScript  

D - constructors for structs

reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I'm wondering if someone will explain to me again what the logic is =
behind not allowing constructors for structs?

I want to do this:

// Test to manipulate 3D vectors, in D!
// This code is released without any warranty. Use at your own risk.
import stream;
import string;
import c.stdio;

struct Vc3
{
    float x,y,z;

    this(float X, float Y, float Z) { x =3D X; y =3D Y; z =3D Z; }

    Vc3 add(Vc3 b) { Vc3 r; r.x =3D x + b.x; r.y =3D y + b.y; r.z =3D z =
+ b.z; return r; }
    Vc3 sub(Vc3 b) { Vc3 r; r.x =3D x - b.x; r.y =3D y - b.y; r.z =3D z =
- b.z; return r; }
    Vc3 mul(float b) { Vc3 r; r.x =3D x * b; r.y =3D y * b; r.z =3D z * =
b; return r; }
    Vc3 div(float b) { return *this * (1/b); }
    void print() { printf("%f %f %f\n", x,y,z); }
}

int main(char[][] args)
{
    Vc3 a,b,c;
    a =3D Vc3(0,1,0);
    a.print();
    Sleep(1000);
    return 0;
}

In general, I want the language to make things easier, not harder.

Sean
Oct 19 2002
next sibling parent reply Patrick Down <pat codemoon.com> writes:
"Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
news:aos9ij$2ht8$1 digitaldaemon.com: 

 I'm wondering if someone will explain to me again what the logic is
 behind not allowing constructors for structs? 
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs. I understand but I too would like to be able to declare and initialize in one line. Perhaps if instead you could call a function on the struct in the declaration statment. Vector3D v1.init(1,2,3);
Oct 19 2002
next sibling parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
That'd work, as a nice workaround for not having constructors.   Some kind
of "initializer" function callable from the declaration.

I don't need destructors for structs nearly as much as I need constructors.
It's just syntax sugar for lots of individual assignments anyway.  Sugar is
good though.

Sean


"Patrick Down" <pat codemoon.com> wrote in message
news:Xns92AC9CA96B771patcodemooncom 63.105.9.61...
 "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
 news:aos9ij$2ht8$1 digitaldaemon.com:

 I'm wondering if someone will explain to me again what the logic is
 behind not allowing constructors for structs?
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs. I understand but I too would like to be able to declare and initialize in one line. Perhaps if instead you could call a function on the struct in the declaration statment. Vector3D v1.init(1,2,3);
Oct 19 2002
parent reply "Dario" <supdar yahoo.com> writes:
This won't warn you about forgetting to initializing a struct, and you can
always write "StructType structName; structName.init();". It's just more
typing.
Anyway, a struct is intended to be a simple object, which usually doesn't
require a sophisticated construction.

I'd like to write "StructType structName = {/*...*/};" even if structName
isn't a static struct. Does this increase the language complexity too much?
I think it's easy to implement. Maybe also:

struct A {int a;}
extern int blah(A);
blah({a: 15});

 That'd work, as a nice workaround for not having constructors.   Some kind
 of "initializer" function callable from the declaration.

 I don't need destructors for structs nearly as much as I need
constructors.
 It's just syntax sugar for lots of individual assignments anyway.  Sugar
is
 good though.

 Sean
 I'm wondering if someone will explain to me again what the logic is
 behind not allowing constructors for structs?
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs. I understand but I too would like to be able to declare and initialize in one line. Perhaps if instead you could call a function on the struct in the declaration statment. Vector3D v1.init(1,2,3);
Oct 20 2002
parent "Sandor Hojtsy" <hojtsy index.hu> writes:
"Dario" <supdar yahoo.com> wrote in message
news:aouokt$1rg5$1 digitaldaemon.com...
 This won't warn you about forgetting to initializing a struct, and you can
 always write "StructType structName; structName.init();". It's just more
 typing.
 Anyway, a struct is intended to be a simple object, which usually doesn't
 require a sophisticated construction.
A constructor can be handy to do non-sophisticated construction too.
Oct 21 2002
prev sibling next sibling parent "Sandor Hojtsy" <hojtsy index.hu> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns92AC9CA96B771patcodemooncom 63.105.9.61...
 "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
 news:aos9ij$2ht8$1 digitaldaemon.com:

 I'm wondering if someone will explain to me again what the logic is
 behind not allowing constructors for structs?
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs.
IMHO not too good reasoning.
 I understand but I too would like to be able to declare and
 initialize in one line.

 Perhaps if instead you could call a function on the struct
 in the declaration statment.

 Vector3D v1.init(1,2,3);
That is only an other syntax for constructors. And a not too good syntax, since it is too similar to function declaration/function call, after all. Why do we need work-arounds instead of a straight clear solution? Sandor
Oct 21 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Patrick Down" <pat codemoon.com> wrote in message
news:Xns92AC9CA96B771patcodemooncom 63.105.9.61...
 "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
 news:aos9ij$2ht8$1 digitaldaemon.com:
 I'm wondering if someone will explain to me again what the logic is
 behind not allowing constructors for structs?
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs.
Constructors for structs will produce a string, that when pulled, will force the inclusion of copy constructors and assignment operator overloading, both of which there's good reason to avoid.
Oct 22 2002
parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Well then can we at least have syntax sugar for calling initializer =
functions from the declaration?

mystruct a.init(x,y,z);

But then what about sending a new struct object to a function?

DrawLine(Point(x1,y1), Point(x2,y2), Color(255,0,0,255));

I read somewhere not long ago (think it was in Lakos) that there's =
overlap in C++ between:

            destroy  init    copy   =20
ctor                   X
copy ctor              X       X
assign         X       X       X
dtor           X

you can make assignment out of the dtor plus the copy ctor
Or, you can ideally break it down into the 3 main operations:  init, =
copy, destroy

Data is more than the sum of its bits.

Sean

"Walter" <walter digitalmars.com> wrote in message =
news:ap32mr$96b$1 digitaldaemon.com...
=20
 "Patrick Down" <pat codemoon.com> wrote in message
 news:Xns92AC9CA96B771patcodemooncom 63.105.9.61...
 "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
 news:aos9ij$2ht8$1 digitaldaemon.com:
 I'm wondering if someone will explain to me again what the logic =
is
 behind not allowing constructors for structs?
I think that Walter beleves that adding contructors will create a chain reaction leading to people wanting destructors and copy constructors for structs.
=20 Constructors for structs will produce a string, that when pulled, will =
force
 the inclusion of copy constructors and assignment operator =
overloading, both
 of which there's good reason to avoid.
Oct 22 2002
parent "Walter" <walter digitalmars.com> writes:
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Those are good ideas to think about.
  "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message =
news:ap40qg$39k$1 digitaldaemon.com...
  Well then can we at least have syntax sugar for calling initializer =
functions from the declaration?

  mystruct a.init(x,y,z);

  But then what about sending a new struct object to a function?

  DrawLine(Point(x1,y1), Point(x2,y2), Color(255,0,0,255));

  I read somewhere not long ago (think it was in Lakos) that there's =
overlap in C++ between:

              destroy  init    copy   =20
  ctor                   X
  copy ctor              X       X
  assign         X       X       X
  dtor           X

  you can make assignment out of the dtor plus the copy ctor
  Or, you can ideally break it down into the 3 main operations:  init, =
copy, destroy

  Data is more than the sum of its bits.

  Sean

  "Walter" <walter digitalmars.com> wrote in message =
news:ap32mr$96b$1 digitaldaemon.com...
  >=20
  > "Patrick Down" <pat codemoon.com> wrote in message
  > news:Xns92AC9CA96B771patcodemooncom 63.105.9.61...
  > > "Sean L. Palmer" <seanpalmer directvinternet.com> wrote in
  > > news:aos9ij$2ht8$1 digitaldaemon.com:
  > > > I'm wondering if someone will explain to me again what the logic =
is
  > > > behind not allowing constructors for structs?
  > > I think that Walter beleves that adding contructors will create
  > > a chain reaction leading to people wanting destructors
  > > and copy constructors for structs.
  >=20
  > Constructors for structs will produce a string, that when pulled, =
will force
  > the inclusion of copy constructors and assignment operator =
overloading, both
  > of which there's good reason to avoid.
Oct 22 2002
prev sibling parent reply Mac Reiter <Mac_member pathlink.com> writes:
In article <aos9ij$2ht8$1 digitaldaemon.com>, Sean L. Palmer says...
I'm wondering if someone will explain to me again what the logic is =
behind not allowing constructors for structs?

I want to do this:

// Test to manipulate 3D vectors, in D!
// This code is released without any warranty. Use at your own risk.
import stream;
import string;
import c.stdio;

struct Vc3
{
    float x,y,z;

    this(float X, float Y, float Z) { x =3D X; y =3D Y; z =3D Z; }

    Vc3 add(Vc3 b) { Vc3 r; r.x =3D x + b.x; r.y =3D y + b.y; r.z =3D z =
+ b.z; return r; }
    Vc3 sub(Vc3 b) { Vc3 r; r.x =3D x - b.x; r.y =3D y - b.y; r.z =3D z =
- b.z; return r; }
    Vc3 mul(float b) { Vc3 r; r.x =3D x * b; r.y =3D y * b; r.z =3D z * =
b; return r; }
    Vc3 div(float b) { return *this * (1/b); }
    void print() { printf("%f %f %f\n", x,y,z); }
}

int main(char[][] args)
{
    Vc3 a,b,c;
    a =3D Vc3(0,1,0);
    a.print();
    Sleep(1000);
    return 0;
}

In general, I want the language to make things easier, not harder.

Sean
Why is Vc3 a struct instead of a class? It has all of the attributes of a class, so it should be called a class. Structs are for small, functionless collections of data. Since you'll be using a LOT of Vc3's, you'll want to make all those functions non-virtual, but going to a struct isn't the way to do it. I unfortunately don't remember the syntax right now. I agree that an inline initialization method would be nice, possibly even necessary, for robust programming. But I do not want to see structs become "classes with a default access mode of public" like they are in C++. If we have two keywords, they should signify two different semantic concepts. If it is useless to have a struct that is that different from a class, then let's just get rid of struct entirely and make sure that we can make class work the way we need it to. Mac
Oct 21 2002
next sibling parent Patrick Down <pat codemoon.com> writes:
Mac Reiter <Mac_member pathlink.com> wrote in
news:ap16lh$1aiu$1 digitaldaemon.com: 


 
 Why is Vc3 a struct instead of a class?  
I won't speak for Sean but I would desire to implement a 3D vector with structs because I would want it to be a base type with value semantics not reference semantics like classes.
Oct 21 2002
prev sibling next sibling parent "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
Vc3 needs to be a struct because it's inherently a value type.

It's a basic type just like complex.

I wouldn't mind if struct and class were somehow integrated but it would
mean the compiler would have to be smart about whether to put stuff on the
stack or on the heap.  These Vc3 are usually embedded directly within some
other class.

Sean

"Mac Reiter" <Mac_member pathlink.com> wrote in message
news:ap16lh$1aiu$1 digitaldaemon.com...
 In article <aos9ij$2ht8$1 digitaldaemon.com>, Sean L. Palmer says...
I'm wondering if someone will explain to me again what the logic is =
behind not allowing constructors for structs?

I want to do this:

// Test to manipulate 3D vectors, in D!
// This code is released without any warranty. Use at your own risk.
import stream;
import string;
import c.stdio;

struct Vc3
{
    float x,y,z;

    this(float X, float Y, float Z) { x =3D X; y =3D Y; z =3D Z; }

    Vc3 add(Vc3 b) { Vc3 r; r.x =3D x + b.x; r.y =3D y + b.y; r.z =3D z =
+ b.z; return r; }
    Vc3 sub(Vc3 b) { Vc3 r; r.x =3D x - b.x; r.y =3D y - b.y; r.z =3D z =
- b.z; return r; }
    Vc3 mul(float b) { Vc3 r; r.x =3D x * b; r.y =3D y * b; r.z =3D z * =
b; return r; }
    Vc3 div(float b) { return *this * (1/b); }
    void print() { printf("%f %f %f\n", x,y,z); }
}

int main(char[][] args)
{
    Vc3 a,b,c;
    a =3D Vc3(0,1,0);
    a.print();
    Sleep(1000);
    return 0;
}

In general, I want the language to make things easier, not harder.

Sean
Why is Vc3 a struct instead of a class? It has all of the attributes of a class, so it should be called a class. Structs are for small,
functionless
 collections of data.  Since you'll be using a LOT of Vc3's, you'll want to
make
 all those functions non-virtual, but going to a struct isn't the way to do
it.
 I unfortunately don't remember the syntax right now.

 I agree that an inline initialization method would be nice, possibly even
 necessary, for robust programming.  But I do not want to see structs
become
 "classes with a default access mode of public" like they are in C++.  If
we have
 two keywords, they should signify two different semantic concepts.  If it
is
 useless to have a struct that is that different from a class, then let's
just
 get rid of struct entirely and make sure that we can make class work the
way we
 need it to.

 Mac
Oct 21 2002
prev sibling parent "Sandor Hojtsy" <hojtsy index.hu> writes:
"Mac Reiter" <Mac_member pathlink.com> wrote in message
news:ap16lh$1aiu$1 digitaldaemon.com...
 In article <aos9ij$2ht8$1 digitaldaemon.com>, Sean L. Palmer says...
I'm wondering if someone will explain to me again what the logic is =
behind not allowing constructors for structs?

I want to do this:

// Test to manipulate 3D vectors, in D!
// This code is released without any warranty. Use at your own risk.
import stream;
import string;
import c.stdio;

struct Vc3
{
    float x,y,z;

    this(float X, float Y, float Z) { x =3D X; y =3D Y; z =3D Z; }

    Vc3 add(Vc3 b) { Vc3 r; r.x =3D x + b.x; r.y =3D y + b.y; r.z =3D z =
+ b.z; return r; }
    Vc3 sub(Vc3 b) { Vc3 r; r.x =3D x - b.x; r.y =3D y - b.y; r.z =3D z =
- b.z; return r; }
    Vc3 mul(float b) { Vc3 r; r.x =3D x * b; r.y =3D y * b; r.z =3D z * =
b; return r; }
    Vc3 div(float b) { return *this * (1/b); }
    void print() { printf("%f %f %f\n", x,y,z); }
}

int main(char[][] args)
{
    Vc3 a,b,c;
    a =3D Vc3(0,1,0);
    a.print();
    Sleep(1000);
    return 0;
}

In general, I want the language to make things easier, not harder.

Sean
Why is Vc3 a struct instead of a class? It has all of the attributes of a class, so it should be called a class. Structs are for small,
functionless
 collections of data.
Says who? I would like to use structs for small (few hundred bytes) function-rich collections of data, which are *fast* to access.
 Since you'll be using a LOT of Vc3's, you'll want to make
 all those functions non-virtual, but going to a struct isn't the way to do
it. Since you'll be using a LOT of Vc3's you would like to avoid the indirection all the time you access the data. Going to a struct is the way to do it. I think structs are/should be the best choice in this example.
 I agree that an inline initialization method would be nice, possibly even
 necessary, for robust programming.  But I do not want to see structs
become
 "classes with a default access mode of public" like they are in C++.  If
we have
 two keywords, they should signify two different semantic concepts.  If it
is
 useless to have a struct that is that different from a class, then let's
just
 get rid of struct entirely and make sure that we can make class work the
way we
 need it to.
You could still use classes when you need initialization, but then you get the overhead of dynamic memory allocation, and a level of indirection throught the reference. So you can choose between robust-and-slow or dirty-and-fast. C++ showed that you can have the best of both world by stack objects with constructors. Now stack objects in D are the structs. So the conclusion is clear: implement constructors into structs. That is robust-and-fast. I think the important concept behind structs is not the lack of constructor, but allocation on the stack. Why else would you ever need struct? BTW, for real robustness you will need access specificators and destructors too. None of them decreases execution speed. And yes I am aware that they increase compiler complexity. But hopefully there will be more people using D, than the ones writing a compiler for it. Sandor
Oct 21 2002