Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 19, 2002 constructors for structs | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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 = X; y = Y; z = Z; } Vc3 add(Vc3 b) { Vc3 r; r.x = x + b.x; r.y = y + b.y; r.z = z + b.z; return r; } Vc3 sub(Vc3 b) { Vc3 r; r.x = x - b.x; r.y = y - b.y; r.z = z - b.z; return r; } Vc3 mul(float b) { Vc3 r; r.x = x * b; r.y = y * b; r.z = 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 = Vc3(0,1,0); a.print(); Sleep(1000); return 0; } In general, I want the language to make things easier, not harder. Sean |
October 19, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | "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); |
October 19, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | 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); > > |
October 20, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | 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); |
October 21, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dario | "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. |
October 21, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | "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 |
October 21, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean L. Palmer | 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 |
October 21, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mac Reiter | 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. |
October 21, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mac Reiter | 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 > > |
October 22, 2002 Re: constructors for structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mac Reiter | "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 |
Copyright © 1999-2021 by the D Language Foundation