digitalmars.D.learn - struct throws errors if it has a DTor
- Namespace (2/2) Dec 08 2012 Why this code compiles without Dtor, but not when I have one?
- Maxim Fomin (14/16) Dec 08 2012 Reading compiler errors is a good idea:
- Namespace (4/21) Dec 08 2012 I have read the error messages, but they have made no sense to
- bearophile (7/11) Dec 08 2012 Since recently this error message was even worse, without the
- bearophile (16/17) Dec 08 2012 import std.stdio;
- Druzhinin Alexandr (11/13) Dec 08 2012 because you have const here:
Why this code compiles without Dtor, but not when I have one? http://dpaste.dzfl.pl/2792b974
Dec 08 2012
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/2792b974Reading 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
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: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. ;)Why this code compiles without Dtor, but not when I have one? http://dpaste.dzfl.pl/2792b974Reading 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
Namespace:...(18-25):Error: function c739.Color.opAssign (Color p) is not callable using argument types (Color) constI 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
Namespace:http://dpaste.dzfl.pl/2792b974import 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
On 08.12.2012 18:10, Namespace wrote:Why this code compiles without Dtor, but not when I have one? http://dpaste.dzfl.pl/2792b974because 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