www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting rid of const/immutable

reply Cecil Ward <d cecilward.com> writes:
I have a particular type name and that type may or may not be 
const and/or immutable. How do I make a new type based on this 
that is mutable, ie getting rid of both const and immutable, but 
not knowing what the original type is ?

I don’t want to repeat information from the definition of the 
original type as this would introduce a bug if the original 
definition is later changed.


Something like

alias immutable_cash_t = immutable(float);
alias mutable_cash_t = float;

// better, in case the original were ever to be changed from 
‘float’ to ‘real’ some day
alias mutable_cash_t = GetRidOfImmutable!( GetRidOfConst!( 
mutable_cash_t ) );
Sep 15 2019
parent Dominikus Dittes Scherkl <dominikus.scherkl continental-corporation.com> writes:
On Monday, 16 September 2019 at 05:22:14 UTC, Cecil Ward wrote:
 I have a particular type name and that type may or may not be 
 const and/or immutable. How do I make a new type based on this 
 that is mutable, ie getting rid of both const and immutable, 
 but not knowing what the original type is ?

 I don’t want to repeat information from the definition of the 
 original type as this would introduce a bug if the original 
 definition is later changed.


 Something like

 alias immutable_cash_t = immutable(float);
 alias mutable_cash_t = float;

 // better, in case the original were ever to be changed from 
 ‘float’ to ‘real’ some day
 alias mutable_cash_t = GetRidOfImmutable!( GetRidOfConst!( 
 mutable_cash_t ) );
If T is you const or immutable type, Unqual!T should be what you want (from std.traits) but this will also remove shared and I don't know what other modifiers there are.
Sep 15 2019