www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18561] New: postblit behaves inconsistently with constants

https://issues.dlang.org/show_bug.cgi?id=18561

          Issue ID: 18561
           Summary: postblit behaves inconsistently with constants
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: Ajieskola gmail.com

This compiles:

import std.array;
import std.stdio;
import std.algorithm;
struct placeAtWorldMap
{   char[] title;
    int[2] coordsMicroDeg;

    this(this)
    {   title = title.dup;
    }
}
void main()
{   char[] title = "London bridge".dup;
    const place = placeAtWorldMap(title, [51_508_038, -87_693]);
    const samePlace = place;
    "falling down ".copy(title);
    place.title.writeln; // falling down
    samePlace.title.writeln; // London bridge
    readln;
}

This does not:

import std.array;
import std.stdio;
import std.algorithm;
struct placeAtWorldMap
{   const char[] title;
    int[2] coordsMicroDeg;

    this(char[] name, int[2] coords)
    {   // you can assign const members here
        this.title = name; 
        this.coordsMicroDeg = coords;
    }

    this(this)
    {   // remove to compile
        title = title.dup;    // cannot modify ´const´ expression
´this.title´
    }
}

void main()
{   char[] title = "London bridge".dup;
    const place = placeAtWorldMap(title, [51_508_038, -87_693]);
    const newPlace = placeAtWorldMap("Big Ben".dup, [51_500_749, -124_611]);
    const samePlace = place;
    "falling down ".copy(title);
    place.title.writeln; // falling down
    samePlace.title.writeln; // London bridge (but falling down without the
postblit causing the error)
    newPlace.title.writeln;      // Big Ben
}

Postblits behave inconsistently with constants. As shown in the second example,
it cannot modify members which are declared as const. But if that is wrong, the
first one should compile neither, as all the members should be treated as const
when the whole variable is const.

Postblits should behave either like member functions -no mutation of const
whatsover- or like constructors -allow mutating the variable once-.

I tend to think it should be the latter of the two, since postblits are
initializing an object, just like constructiors.

--
Mar 06 2018