DIP 1016--ref T accepts r-values--Community Review Round 1

Seb seb at wilzba.ch
Fri Jul 20 11:43:29 UTC 2018


On Friday, 20 July 2018 at 10:43:54 UTC, Dgame wrote:
> On Friday, 20 July 2018 at 10:31:48 UTC, Seb wrote:
>> On Friday, 20 July 2018 at 10:08:03 UTC, Dgame wrote:
>>> On Friday, 20 July 2018 at 09:39:47 UTC, Nicholas Wilson 
>>> wrote:
>>>> On Friday, 20 July 2018 at 09:03:18 UTC, Dukc wrote:
>>>>> appending something (like .byRef or byRef!long, the latter 
>>>>> making an implicit type conversion)
>>>>
>>>> That can't work: either it returns an expired stack 
>>>> temporary (*very* bad), or allocates with no way to 
>>>> deallocate (bad).
>>>
>>> What about something like this?
>>>
>>> ----
>>> import std.stdio;
>>>
>>> ref T byRef(T)(T value) {
>>>     static T _val = void;
>>>     _val = value;
>>>
>>>     return _val;
>>> }
>>>
>>> void foo(ref int a) {
>>>     writeln("A = ", a);
>>> }
>>>
>>> void main() {
>>>     foo(42.byRef);
>>>     foo(23.byRef);
>>> }
>>> ----
>>
>> That can't work, consider e.g.:
>>
>> foo(10.byRef, 20.byRef); // calls foo with (20, 20)
>>
>> https://run.dlang.io/is/lazeu2
>
> True.. But this could work (but is way more uglier): 
> https://run.dlang.io/is/rKs2yQ


Putting the missing syntax sugar aside and performance overhead 
due to unneeded copies, this won't work either.
Consider for example that A could be a non-copyable type:

---
struct A {
     int i;
     @disable this(this);
}
---

With this DIP it should be possible to do `foo(A(1))` because 
only a reference is passed around.


More information about the Digitalmars-d mailing list