Thread overview
Casting rules
Aug 26, 2022
JG
Aug 26, 2022
ag0aep6g
Aug 27, 2022
JG
August 26, 2022

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?

August 26, 2022

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.

August 27, 2022

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.