Re: Casting rules

2022-08-27 Thread JG via Digitalmars-d-learn

On Friday, 26 August 2022 at 21:18:15 UTC, ag0aep6g wrote:

On Friday, 26 August 2022 at 20:42:07 UTC, JG wrote:

[...]


Casting immutable/const away: 
https://dlang.org/spec/const3.html#removing_with_cast


The cast itself allowed. Mutating the data is not.

Casting to immutable: 
https://dlang.org/spec/const3.html#creating_immutable_data


The cast is allowed as long as you don't mutate the data 
afterwards.


Conversion to const doesn't need a cast. Mutable and immutable 
both implicitly convert to const.



Thank you very much.


Re: Casting rules

2022-08-26 Thread ag0aep6g via Digitalmars-d-learn

On Friday, 26 August 2022 at 20:42:07 UTC, JG wrote:
Where can I find rules about casting. e.g. I assume casting 
away immutable is undefined behavior (or implementation defined 
behavior). What about casting to immutable (I would expect at 
most it only to be allowed if your type has no references e.g. 
ints okay but int[] not etc.) Casting const away (probably not 
allowed)? Casting to const (probably allowed).


Anyhow guessing aside where can I find the rules?


Casting immutable/const away: 
https://dlang.org/spec/const3.html#removing_with_cast


The cast itself allowed. Mutating the data is not.

Casting to immutable: 
https://dlang.org/spec/const3.html#creating_immutable_data


The cast is allowed as long as you don't mutate the data 
afterwards.


Conversion to const doesn't need a cast. Mutable and immutable 
both implicitly convert to const.