www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct throws errors if it has a DTor

reply "Namespace" <rswhite4 googlemail.com> writes:
Why this code compiles without Dtor, but not when I have one?

http://dpaste.dzfl.pl/2792b974
Dec 08 2012
next sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Saturday, 8 December 2012 at 11:10:27 UTC, Namespace wrote:
 Why this code compiles without Dtor, but not when I have one?

 http://dpaste.dzfl.pl/2792b974
Reading compiler errors is a good idea: (18-25):Error: function c739.Color.opAssign (Color p) is not callable using argument types (Color) const Because you use didn't supply opAssign, default one is generated. It calls postblit and destructor for temporary. Once you define any of them, default opAssign cannot be const and because you assign static const structs in static constructor at lines 18-25, compiler issues errors. Solution is to move initialization of static fields to compile time and use static struct initialization. In any case you know values of Black, Green, etc. so why would one initialize them at runtime? You can also turn them into enums. http://dpaste.dzfl.pl/09572204
Dec 08 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 8 December 2012 at 13:09:39 UTC, Maxim Fomin wrote:
 On Saturday, 8 December 2012 at 11:10:27 UTC, Namespace wrote:
 Why this code compiles without Dtor, but not when I have one?

 http://dpaste.dzfl.pl/2792b974
Reading compiler errors is a good idea: (18-25):Error: function c739.Color.opAssign (Color p) is not callable using argument types (Color) const Because you use didn't supply opAssign, default one is generated. It calls postblit and destructor for temporary. Once you define any of them, default opAssign cannot be const and because you assign static const structs in static constructor at lines 18-25, compiler issues errors. Solution is to move initialization of static fields to compile time and use static struct initialization. In any case you know values of Black, Green, etc. so why would one initialize them at runtime? You can also turn them into enums. http://dpaste.dzfl.pl/09572204
I have read the error messages, but they have made no sense to me. It just seemed to be one of many bugs of D. But now it's clear. Thank you. ;)
Dec 08 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 (18-25):Error: function c739.Color.opAssign (Color p) is not 
 callable using argument types (Color) const
...
 I have read the error messages, but they have made no sense to 
 me.
Since recently this error message was even worse, without the "const" at the end. I've asked for a further improvement of this error message. Bye, bearophile
Dec 08 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 http://dpaste.dzfl.pl/2792b974
import std.stdio; struct Color { static immutable Color Black = Color(0, 0, 0), White = Color(255, 255, 255), Red = Color(255, 0, 0), Green = Color(0, 255, 0), Blue = Color(0, 0, 255), Yellow = Color(0, 255, 255), Magenta = Color(255, 0, 255), Gray = Color(0.7, 0.7, 0.7); ubyte red, green, blue, alpha; this(ubyte red, ubyte green, ubyte blue, ubyte alpha = 0) { ... Bye, bearophile
Dec 08 2012
prev sibling parent Druzhinin Alexandr <news digitalmars.com> writes:
On 08.12.2012 18:10, Namespace wrote:
 Why this code compiles without Dtor, but not when I have one?

 http://dpaste.dzfl.pl/2792b974
because you have const here: static const Color Black; static const Color White; static const Color Red; static const Color Green; static const Color Blue; static const Color Yellow; static const Color Magenta; static const Color Gray; Alexandr
Dec 08 2012