-preview=in might break code

Timon Gehr timon.gehr at gmx.ch
Sat Oct 3 11:04:48 UTC 2020


On 02.10.20 22:06, Steven Schveighoffer wrote:
> On 10/2/20 3:54 PM, tsbockman wrote:
>> On Friday, 2 October 2020 at 14:48:18 UTC, Steven Schveighoffer wrote:
>>> I think in should always mean ref. If you want to pass not by ref, 
>>> use const.
>>
>> Please do not kill this optimization. There is currently no other 
>> reasonable way available in the D language to express the idea of 
>> "pass by reference or by value, whichever is faster". But, there is an 
>> easy way to require that a parameter always be passed in a specific 
>> way: just don't use `in` and use `scope const` or `scope const ref` 
>> directly.
>>
>> Implementing this optimization manually is quite painful in D; the 
>> possibilities are complex and very ugly, and require detailed 
>> knowledge of the platform's ABI to implement correctly in the general 
>> case.
>>
>> D is a systems programming language, and should not kill useful 
>> features just because they have potential pitfalls. At most, 
>> problematic features may be blocked by @safe/linters/style guides, but 
>> please don't remove them entirely.
> 
> The only way this feature can stay the way it is is to add to the 
> undefined behavior of the language:
> ...

No. UB means demons may fly out of your nose. It's not that. You just 
get one of two behaviors, one is pass-by-reference, the other is 
pass-by-value.

> "It is undefined behavior to pass a mutable argument to an `in` 
> parameter, and to read from that parameter after that mutable data has 
> been modified while executing the function."
> 
> If everyone is OK with that, then it can stay the way it is. Otherwise, 
> the spec has to be more specific about whether it's expected to be ref 
> or not ref.
> 
> -Steve

There's a difference between "the behavior may be either A or B" and 
"the behavior may be anything you like"...


More information about the Digitalmars-d mailing list