December 08, 2012
Why this code compiles without Dtor, but not when I have one?

http://dpaste.dzfl.pl/2792b974
December 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/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
December 08, 2012
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
December 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/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
December 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:
>> 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. ;)

December 08, 2012
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
Top | Discussion index | About this forum | D home