Is this a violation of const?

Salih Dincer salihdb at hotmail.com
Sat Jul 30 19:49:52 UTC 2022


On Saturday, 30 July 2022 at 10:34:09 UTC, ag0aep6g wrote:
>
> You're not making sense. Your `s` is mutable, not immutable.

You're right! I saw the hole at the end of the tunnel late 😀

But if you compile the example below without the `new operator`, 
the system does not work and does not give any errors.  Why?

**Voldermort Type Version:**
```d
auto imstr(string str) pure @safe
{
   struct IMSTR
   {
     string s;
     void delegate(string s) @safe update;
     string toString() const { return s; }
   }
   auto s = new IMSTR(str);
        s.update = (_) { s.s = _; };
   return s;
}

import std.stdio;
void main() @safe
{
   immutable auto str = imstr("Test 123");
   //str.s = "test";

   str.toString.writeln;
   str.update("TEST A");

   str.toString.writeln;
   str.update("TEST B");

   str.toString.writeln;
   typeid(str).writeln;

}/* Prints:
Test 123
TEST A
TEST B
immutable(immutable(onlineapp.imstr(immutable(char)[]).IMSTR)*)
*/
```
SDB at 79



More information about the Digitalmars-d-learn mailing list