digitalmars.D.learn - distinguish between classes and structures
- Weed (3/3) Dec 14 2008 Who can provide a link to an explanation about why in D has taken to
- Bill Baxter (5/8) Dec 14 2008 The main justification is eliminating the slicing problem.
- Weed (3/15) Dec 14 2008 Why not disallow the casting for structs, leaving the possibility of up
- Weed (2/21) Dec 15 2008 What is the best place for such questions?
- Bill Baxter (23/23) Dec 15 2008 MjAwOC8xMi8xNSBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gV2VlZCDQydvF1DoKPj4K...
- Bill Baxter (14/14) Dec 15 2008 MjAwOC8xMi8xNSBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gQmlsbCBCYXh0ZXIg0Mnb...
- Weed (15/36) Dec 15 2008 Sorry, English is not my native language
- Weed (4/6) Dec 15 2008 It's a question:
- Kagamin (2/4) Dec 15 2008 for example?
- Weed (6/12) Dec 15 2008
- Frits van Bommel (5/22) Dec 15 2008 This is why:
- Weed (5/28) Dec 15 2008 Yes, right.
- Bill Baxter (23/23) Dec 15 2008 MjAwOC8xMi8xNiBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gRnJpdHMgdmFuIEJvbW1l...
Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)
Dec 14 2008
On Mon, Dec 15, 2008 at 3:35 PM, Weed <resume755 mail.ru> wrote:Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type. --bb
Dec 14 2008
Bill Baxter пишет:On Mon, Dec 15, 2008 at 3:35 PM, Weed <resume755 mail.ru> wrote:Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure?Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type.
Dec 14 2008
Weed пишет:Bill Baxter пишет:What is the best place for such questions?On Mon, Dec 15, 2008 at 3:35 PM, Weed <resume755 mail.ru> wrote:Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure?Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-pro lem-class-base.html D solves it by making it impossible to have a class instance as a value type.
Dec 15 2008
MjAwOC8xMi8xNSBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gV2VlZCDQydvF1DoKPj4KPj4g QmlsbCBCYXh0ZXIg0MnbxdQ6Cj4+Pgo+Pj4gT24gTW9uLCBEZWMgMTUsIDIwMDggYXQgMzozNSBQ TSwgV2VlZCA8cmVzdW1lNzU1QG1haWwucnU+IHdyb3RlOgo+Pj4+Cj4+Pj4gV2hvIGNhbiBwcm92 aWRlIGEgbGluayB0byBhbiBleHBsYW5hdGlvbiBhYm91dCB3aHkgaW4gRCBoYXMgdGFrZW4gdG8K Pj4+PiBkaXN0aW5ndWlzaCBiZXR3ZWVuIGNsYXNzZXMgYW5kIHN0cnVjdHVyZXM/Cj4+Pj4KPj4+ PiAoU29ycnkgZm9yIG15IGJhZCBFbmdsaXNoKQo+Pj4+Cj4+Pgo+Pj4gVGhlIG1haW4ganVzdGlm aWNhdGlvbiBpcyBlbGltaW5hdGluZyB0aGUgc2xpY2luZyBwcm9ibGVtLgo+Pj4KPj4+IGh0dHA6 Ly9jcGx1c3BsdXNnZW1zLmJsb2dzcG90LmNvbS8yMDA1LzEwL3doYXQtaXMtc2xpY2luZy1wcm9i bGVtLWNsYXNzLWJhc2UuaHRtbAo+Pj4KPj4+IEQgc29sdmVzIGl0IGJ5IG1ha2luZyBpdCBpbXBv c3NpYmxlIHRvIGhhdmUgYSBjbGFzcyBpbnN0YW5jZSBhcyBhIHZhbHVlCj4+PiB0eXBlLgo+Pj4K Pj4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++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // errorBill Baxter пишет:I have to confess I don't really understand this question. Can you rephrase or give an example?On Mon, Dec 15, 2008 at 3:35 PM, Weed <resume755 mail.ru> wrote:Why not disallow the casting for structs, leaving the possibility of up casting for the links and pointers to the structure?Who can provide a link to an explanation about why in D has taken to distinguish between classes and structures? (Sorry for my bad English)The main justification is eliminating the slicing problem. http://cplusplusgems.blogspot.com/2005/10/what-is-slicing-problem-class-base.html D solves it by making it impossible to have a class instance as a value type.
Dec 15 2008
Weed пишет:Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value.It's a question: Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value?
Dec 15 2008
Weed Wrote:Without inheritance of structs many things are not possible, compared with C++.for example?
Dec 15 2008
Kagamin пишет:Weed Wrote:it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type. this is mathematical matrix, for exampleWithout inheritance of structs many things are not possible, compared with C++.for example?
Dec 15 2008
Weed Wrote:it is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type. this is mathematical matrix, for exampleMatix? Easy. class Matrix //can inherit from some generic base class { int[2] dims; int[][] data; //methods }
Dec 16 2008
Kagamin пишет:Weed Wrote:not just a matrix, see thread "struct inheritance need?" in this NGit is impossible to create the struct of the object which will be arbitrary size (raised by a user) + to be able to set compile-time + should work with the structures of its type. this is mathematical matrix, for exampleMatix? Easy. class Matrix //can inherit from some generic base class { int[2] dims; int[][] data; //methods }
Dec 16 2008
Weed wrote:In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // errorThis is why: s1 val2 = *base_ptr; // error (And disallowing '*ptr' on struct pointers is not likely to find much support)
Dec 15 2008
Frits van Bommel пишет:Weed wrote:Yes, right. I do not understand that it is wrong?In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // errorThis is why: s1 val2 = *base_ptr; // error(And disallowing '*ptr' on struct pointers is not likely to find much support)I am not saying do not let '* ptr'. Besides, now you can not do this just because there is no inheritance at all.
Dec 15 2008
MjAwOC8xMi8xNiBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gRnJpdHMgdmFuIEJvbW1lbCDQ ydvF1DoKPj4KPj4gV2VlZCB3cm90ZToKPj4+Cj4+PiBJbiBDKyssIHdlIGhhZCB0aGUgcHJvYmxl bSAtICJzbGljaW5nIiBvYmplY3RzLgo+Pj4gSW4gRCB0aGlzIHByb2JsZW0gaXMgc29sdmVkIGlu YWJpbGl0eSB0byBpbmhlcml0IGZyb20gc3RydWN0cy4KPj4+IFdpdGhvdXQgaW5oZXJpdGFuY2Ug b2Ygc3RydWN0cyBtYW55IHRoaW5ncyBhcmUgbm90IHBvc3NpYmxlLCBjb21wYXJlZAo+Pj4gd2l0 aCBDKysuCj4+PiBXaHksIGluc3RlYWQgb2YgdGhlIGNvbXBsZXRlIGluYWJpbGl0eSB0byBpbmhl cml0LCBqdXN0IGRvIG5vdCBtYWtlCj4+PiBpbXBvc3NpYmxlIHRvIHVwIGNhc3Rpbmcgc3RydWN0 IHR5cGUgYnkgdmFsdWUuCj4+Pgo+Pj4gbGlrZSB0aGlzOgo+Pj4KPj4+IHN0cnVjdCBzMSB7fQo+ Pj4gc3RydWN0IHMyIDogczEge30KPj4+Cj4+P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vb2QuCgotLWJiCg==
Dec 15 2008
Bill Baxter пишет:2008/12/16 Weed <resume755 mail.ru>:This break garbage collection? Or what?Frits van Bommel пишет:The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it.Weed wrote:I do not understand that it is wrong?In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // errorThis is why: s1 val2 = *base_ptr; // errorAny extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type. Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good.
Dec 15 2008
Bill Baxter пишет:2008/12/16 Weed <resume755 mail.ru>:Classes can not be created at compile time. D, with its templates would do it perfectly for structs.Frits van Bommel пишет:The problem is that base_ptr isn't actually pointing to an s1. So by dereferencing it and assigning to an s1 you just sliced it. Any extra data derr had is lost. Any method overrides it had are lost. So to avoid slicing you would need to disallow dereferencing struct pointers, or disallow pointing to a struct of a different type. Disallowing dereferencing a struct pointer seems like it might be possible. But then again that's also kinda what class gives you already. And then you'd be left with no way to create a pointer that can be turned back into a value! That doesn't sound so good. --bbWeed wrote:I do not understand that it is wrong?In C++, we had the problem - "slicing" objects. In D this problem is solved inability to inherit from structs. Without inheritance of structs many things are not possible, compared with C++. Why, instead of the complete inability to inherit, just do not make impossible to up casting struct type by value. like this: struct s1 {} struct s2 : s1 {} s1 base; s2 derr; s1* base_ptr = &derr; // ok s1 val = derr; // errorThis is why: s1 val2 = *base_ptr; // error
Dec 15 2008