Re: Is this a violation of const?

2022-07-30 Thread Salih Dincer via Digitalmars-d-learn
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

Re: Is this a violation of const?

2022-07-30 Thread Timon Gehr via Digitalmars-d-learn
On 7/30/22 15:19, Salih Dincer wrote: On Saturday, 30 July 2022 at 10:02:50 UTC, Timon Gehr wrote: It's a `const` hole, plain and simple. This code, which consists of 26 lines, does not compile in DMD 2.087.  I am getting this error: constHole.d(15): Error: mutable method

Re: Is this a violation of const?

2022-07-30 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 30 July 2022 at 10:02:50 UTC, Timon Gehr wrote: It's a `const` hole, plain and simple. This code, which consists of 26 lines, does not compile in DMD 2.087. I am getting this error: constHole.d(15): Error: mutable method `source.Updater.opCall` is not callable using a

Re: Is this a violation of const?

2022-07-30 Thread ag0aep6g via Digitalmars-d-learn
On 30.07.22 09:15, Salih Dincer wrote: It's possible to do this because it's immutable.  You don't need an extra update() function anyway. ```d void main() {     auto s = S("test A");     s.update = (_) { s.s = _; };     s.update("test B");     assert(s.s == "test B");     s.s = "test

Re: Is this a violation of const?

2022-07-30 Thread Timon Gehr via Digitalmars-d-learn
On 7/30/22 00:16, H. S. Teoh wrote: On Fri, Jul 29, 2022 at 09:56:20PM +, Andrey Zherikov via Digitalmars-d-learn wrote: In the example below `func` changes its `const*` argument. Does this violates D's constness? ```d import std; struct S { string s; void delegate(string s)

Re: Is this a violation of const?

2022-07-30 Thread Salih Dincer via Digitalmars-d-learn
On Saturday, 30 July 2022 at 06:04:16 UTC, ag0aep6g wrote: Yes. Here's a modified example to show that you can also violate `immutable` this way: It's possible to do this because it's immutable. You don't need an extra update() function anyway. ```d void main() { auto s = S("test

Re: Is this a violation of const?

2022-07-30 Thread ag0aep6g via Digitalmars-d-learn
On 29.07.22 23:56, Andrey Zherikov wrote: In the example below `func` changes its `const*` argument. Does this violates D's constness? Yes. Here's a modified example to show that you can also violate `immutable` this way: struct S { string s; void delegate(string s) @safe update; }

Re: Is this a violation of const?

2022-07-29 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 29 July 2022 at 23:15:14 UTC, Salih Dincer wrote: It's smart to use `delegate`, but `immutable` doesn't necessarily mean `const`. So if we use `const char`: ```d struct S { char s; void delegate(char s) update; } ``` Pardon  I forgot the assert test, also writing const

Re: Is this a violation of const?

2022-07-29 Thread Salih Dincer via Digitalmars-d-learn
On Friday, 29 July 2022 at 21:56:20 UTC, Andrey Zherikov wrote: In the example below `func` changes its `const*` argument. Does this violates D's constness? It's smart to use `delegate`, but `immutable` doesn't necessarily mean `const`. So if we use `const char`: ```d struct S {

Re: Is this a violation of const?

2022-07-29 Thread Andrey Zherikov via Digitalmars-d-learn
On Friday, 29 July 2022 at 22:16:26 UTC, H. S. Teoh wrote: This totally makes sense. Thanks for explanation!

Re: Is this a violation of const?

2022-07-29 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jul 29, 2022 at 09:56:20PM +, Andrey Zherikov via Digitalmars-d-learn wrote: > In the example below `func` changes its `const*` argument. Does this > violates D's constness? > > ```d > import std; > > struct S > { > string s; > > void delegate(string s) update; > } > >

Is this a violation of const?

2022-07-29 Thread Andrey Zherikov via Digitalmars-d-learn
In the example below `func` changes its `const*` argument. Does this violates D's constness? ```d import std; struct S { string s; void delegate(string s) update; } void func(const S* s) { writeln(*s); s.update("func"); writeln(*s); } void main() { auto s =