How to cast `shared` away reliably
Paul Backus
snarwin at gmail.com
Wed Feb 11 21:34:14 UTC 2026
On Wednesday, 11 February 2026 at 16:49:19 UTC, Arafel wrote:
> On 2/11/26 16:02, Paul Backus wrote:
>> All type qualifiers in D (`const`, `immutable`, `shared`) are
>> transitive, which means that a type qualifier applied to the
>> "top level" of a type will also be applied recursively to each
>> part of the type. So, a `const(int[])` is the same as a
>> `const(const(int)[])`, and likewise for `shared`, `immutable`,
>> or any combination thereof.
>>
>> This is documented in the language spec's page on type
>> qualifiers:
>>
>>> Type qualifiers modify a type by applying a _TypeCtor_.
>>> _TypeCtors_ are: `const`, `immutable`, `shared`, and `inout`.
>>> Each applies transitively to all subtypes.
>>
>> Source: https://dlang.org/spec/const3.html
>
> Quite helpful even if still somewhat confusing.
>
> I think it would still make sense to define what a "subtype" is.
The usage of the word "subtype" here is incorrect; it should
probably say something like "component types" instead.
> However, of the items listed there, pointers are indeed showing
> this behaviour, but functions and delegates aren't.
What you are referring to as "function types" are probably
function _pointer_ types. Qualifiers work the same way for them
as they do for any other pointer types.
Note that the _parameters_ of a function are not affected by
qualifiers on a function pointer, because the parameters of a
function are neither _contained in_ nor _pointed to_ by the
function pointer.
For delegates, the qualifier _should_ apply to both the function
pointer and the context pointer, but iirc there are some
outstanding bugs in the compiler related to this, so it may not
work correctly in all cases.
More information about the Digitalmars-d
mailing list