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:
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@79