-preview=in might break code

Steven Schveighoffer schveiguy at gmail.com
Fri Oct 2 20:06:34 UTC 2020


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:

"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


More information about the Digitalmars-d mailing list