Odd behavior of darray.dup

Bastiaan Veelo Bastiaan at Veelo.net
Sun Feb 24 12:10:22 UTC 2019

On Saturday, 23 February 2019 at 23:35:14 UTC, solidstate1991 
> On Saturday, 23 February 2019 at 19:21:10 UTC, Bastiaan Veelo 
> wrote:
>> It works for me:
>> https://run.dlang.io/gist/473b0021487275751accaebeb00be05c
>> -- Bastiaan
> Still no luck, not even with memcpy.
> There's even more mystery as I printed out the original static 
> immutable array's content, which is different from the 
> non-immutable one. Instead of the 32bit hex value of the given 
> color in the format of "0x00000000", I get 
> "immutable(Color)(#{overlap raw, colors})". This might be a bug.

Sorry, I didn't realize that my link didn't work (due to issue 

I have instead exported a Gist 
https://gist.github.com/run-dlang/965a2fb793bda7b1a82b0512d78973f3 that you should be able to use with the "Import Gist" button on https://run.dlang.io/. I have included the relevant bits from pixelperfectengine (slightly modified, see below). If you have further questions, you can do the same so it will be easier to see what is going on.

Several things here:

1) You get "immutable(Color)(#{overlap raw, colors})" on 
immutable Color because the `Color.toString()` method is not 
const (corrected in the Gist). This is a bug in 
pixelperfectengine. Likewise, getters like `green()` should be 

2) Also note that `intToHex()` could simply be implemented using 
std.format, as row 127 illustrates.

3) You get all zeros because static data needs a static module 
constructor, added on line 111.

4) As an alternative to static data you could use compile time 
data (line 118) which does not need a constructor nor `.dup`. 
Beware that each time you use it though, its value gets 
copy-pasted into the code like a C #define.

Hope this helps,

More information about the Digitalmars-d-learn mailing list