Is there any analogue of C++ mutable
in D? As far as I understand, this is impossible due to transitive constness, but maybe some variation of Rebindable
will be useful?
P.S. Of course I mean 'fair' mutable, not qualifier-away cast()
.
Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 30, 2021 C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Is there any analogue of C++ P.S. Of course I mean 'fair' mutable, not qualifier-away |
July 30, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Galuza | On Friday, 30 July 2021 at 19:17:55 UTC, Michael Galuza wrote: >Is there any analogue of C++ P.S. Of course I mean 'fair' mutable, not qualifier-away Nope. No logical const, head const, or tail const in D; just pure transitive const. Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). |
July 30, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: >Nope. No logical const, head const, or tail const in D; just pure transitive const. Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). Thank you D for not having "mutable" as in C++. I don't see mutable often in C++ and is one of those corners in C++ that I don't think make much sense. const is const, easy. In C++ const might not be ROMable. In D immutable is ROMable and transistive const helps this (unless there is some escape hatch I don't know about). |
July 31, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to IGotD- | On Friday, 30 July 2021 at 21:53:01 UTC, IGotD- wrote: >On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: >Nope. No logical const, head const, or tail const in D; just pure transitive const. Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). Thank you D for not having "mutable" as in C++. I don't see mutable often in C++ and is one of those corners in C++ that I don't think make much sense. const is const, easy. In C++ const might not be ROMable. In D immutable is ROMable and transistive const helps this (unless there is some escape hatch I don't know about). Well, Jonathan Davies disagrees: |
July 31, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to IGotD- | On Friday, 30 July 2021 at 21:53:01 UTC, IGotD- wrote: >On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: I'm really surprised you think it doesn't make much sense. What about:
|
July 31, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gollan | On Saturday, 31 July 2021 at 12:27:28 UTC, Gollan wrote: >I'm really surprised you think it doesn't make much sense. What about:
They way I look at it if you don't want the code alter the memory location then it must be immutable/const. The way you describe it, it is based on what the API expect to do with the object even if there might be internal memory writes, like the semaphore example. With structures where members are expanded into the structure, then they share the memory location and partial const in my opinion should be prohibited. Now this can be different with classes as members are allocated independently but I'm sure about the practical benefit. It boils down to what we expect const to represent. I used the word ROMable for a reason meaning the allocated memory should not alter. |
August 01, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: >Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). No. |
August 01, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Sunday, 1 August 2021 at 06:15:30 UTC, Jack Applegame wrote: >On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: >Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). No. Not in C++, when the const object modification is taking place via member variables declared as mutable accessed from a const member function. This is also a thing in Swift and Rust, by the way, they just use other mechanisms to do the same. |
August 01, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Galuza | On Friday, 30 July 2021 at 19:17:55 UTC, Michael Galuza wrote: >Is there any analogue of C++ P.S. Of course I mean 'fair' mutable, not qualifier-away Not any good analogue. There are some situation-dependant possibilities though. First one, you could do some template magic. Something like:
Second possibility is that you store the mutable "element" in some external data structure, and store it's key or index in the |
August 01, 2021 Re: C++ mutable in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Sunday, 1 August 2021 at 06:15:30 UTC, Jack Applegame wrote: >On Friday, 30 July 2021 at 20:09:19 UTC, Tejas wrote: >Also remember that casting away const is undefined behaviour in D (unlike being defined and supported in C++). No. In D, casting away const from a pointer and mutating the pointed-to object is UB even if the object it points to was not originally declared as
In C++, the above is allowed. Source: https://dlang.org/spec/cpp_interface.html#comparing-d-immutable-and-const-with-cpp-const |