`in` parameters made useful
tsbockman
thomas.bockman at gmail.com
Fri Aug 21 23:16:53 UTC 2020
On Friday, 21 August 2020 at 19:21:37 UTC, Andrei Alexandrescu
wrote:
> On 8/21/20 1:20 AM, Araq wrote:
>> On Thursday, 20 August 2020 at 22:19:16 UTC, Andrei
>> Alexandrescu wrote:
>>> On 8/20/20 1:31 PM, IGotD- wrote:
>>> void fun(ref S a, const compiler_chooses S b) {
>>> ... mutate a, read b ...
>>> }
>>>
>>> S x;
>>> fun(x, x); // oops
>>>
>>> The problem now is that the semantics of fun depends on
>>> whether the compiler chose pass by value vs. pass by
>>> reference.
>>
>> True but in practice it doesn't happen very often. The
>> benefits far outweigh this minor downside.
>
> That seems quite worrisome. A bug rare and subtle that can
> become devastating. Something the Hindenburg captain might have
> said.
>
>> Plus there are known ways to prevent this form of aliasing at
>> compile-time.
>
> Not if you have globals and/or separate compilation.
The risk of aliasing-related bugs comes from the availability of
pointers/references though, which will still be present in D with
or without this optimization. People who want to rule out
aliasing problems can just explicitly specify pass-by-value using
`scope const` instead of `in`.
Let's not cripple the future of the language out of fear of
pitfalls that are already present, and cannot be entirely removed
as long as D remains a systems programming language.
More information about the Digitalmars-d
mailing list