www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does Unconst exist?

reply Tejas <notrealemail gmail.com> writes:
When I initially saw it, I was hopeful that it would allow me to 
bypass some of the restrictions of ```const``` , but it literally 
just takes a type and strips the ```const``` from it, you can't 
pass a variable to it in order to get rid of ```const``` . What 
use does it serve then?

https://dlang.org/library/std/traits/unconst.html
Jul 27 2021
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/27/21 10:38 PM, Tejas wrote:
 When I initially saw it, I was hopeful that it would allow me to bypass 
 some of the restrictions of ```const``` , but it literally just takes a 
 type and strips the ```const``` from it, you can't pass a variable to it 
 in order to get rid of ```const``` . What use does it serve then?
 
 https://dlang.org/library/std/traits/unconst.html
Searching under /usr/include/dmd reveals that it is used in the implementations of array property 'dup' and 'copyEmplace', which makes sense: We want copies to be mutable. Ali
Jul 27 2021
parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 28 July 2021 at 05:52:02 UTC, Ali Çehreli wrote:
 On 7/27/21 10:38 PM, Tejas wrote:
 When I initially saw it, I was hopeful that it would allow me 
 to bypass some of the restrictions of ```const``` , but it 
 literally just takes a type and strips the ```const``` from 
 it, you can't pass a variable to it in order to get rid of 
 ```const``` . What use does it serve then?
 
 https://dlang.org/library/std/traits/unconst.html
Searching under /usr/include/dmd reveals that it is used in the implementations of array property 'dup' and 'copyEmplace', which makes sense: We want copies to be mutable. Ali
So it's only useful for the compiler developers? I thought Walter was 100% against introducing any dependency to phobos in dmd? Shouldn't this be part of ```core.internal``` package?
Jul 27 2021
prev sibling parent reply user1234 <user1234 12.de> writes:
On Wednesday, 28 July 2021 at 05:38:44 UTC, Tejas wrote:
 When I initially saw it, I was hopeful that it would allow me 
 to bypass some of the restrictions of ```const``` , but it 
 literally just takes a type and strips the ```const``` from it, 
 you can't pass a variable to it in order to get rid of 
 ```const``` . What use does it serve then?
To manipulate types in template metaprogramming. To remove `const` from a variable, `cast() stuff` is shorter than `cast(Unconst!(typeof(stuff))) stuff`, but it also removes `shared` and `immutable.
Jul 27 2021
parent Tejas <notrealemail gmail.com> writes:
On Wednesday, 28 July 2021 at 05:57:31 UTC, user1234 wrote:
 On Wednesday, 28 July 2021 at 05:38:44 UTC, Tejas wrote:
 When I initially saw it, I was hopeful that it would allow me 
 to bypass some of the restrictions of ```const``` , but it 
 literally just takes a type and strips the ```const``` from 
 it, you can't pass a variable to it in order to get rid of 
 ```const``` . What use does it serve then?
To manipulate types in template metaprogramming. To remove `const` from a variable, `cast() stuff` is shorter than `cast(Unconst!(typeof(stuff))) stuff`, but it also removes `shared` and `immutable.
Oh... that makes sense... then it shouldn't exist in ```core.internal``` only, and has a place in phobos. Ignore my last comment.
Jul 27 2021