-preview=in might break code

Max Haughton maxhaton at gmail.com
Fri Oct 2 17:16:46 UTC 2020


On Friday, 2 October 2020 at 14:32:50 UTC, Andrei Alexandrescu 
wrote:
> On 10/2/20 10:08 AM, Steven Schveighoffer wrote:
>> Is there a way to prevent this?
>> 
>> import std.stdio;
>> struct S(size_t elems)
>> {
>>      int[elems] data;
>> }
>> 
>> void foo(T)(in T constdata, ref T normaldata)
>> {
>>      normaldata.data[0] = 1;
>>      writeln(constdata.data[0]);
>> }
>> void main()
>> {
>>      S!1 smallval;
>>      foo(smallval, smallval);
>>      S!100 largeval;
>>      foo(largeval, largeval);
>> }
>> 
>> 
>> Compile without -preview=in, it prints:
>> 
>> 0
>> 0
>> 
>> Compile with -preview=in, it prints:
>> 
>> 0
>> 1
>
> Finally, my "told you so" moment has come! :o)
>
> https://forum.dlang.org/post/rhmst4$1vmc$1@digitalmars.com

Could we be ambitious and aim to have ownership taken to the max 
and catch this statically? This particular case is relatively low 
hanging fruit but having the in parameter work this way would be 
nice if it was safe.




More information about the Digitalmars-d mailing list