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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jul 20 16:33:56 UTC 2018


On Friday, July 20, 2018 15:50:29 meppl via Digitalmars-d wrote:
> On Friday, 20 July 2018 at 13:21:11 UTC, Jonathan M Davis wrote:
> > On Friday, July 20, 2018 05:16:53 Mike Parker via Digitalmars-d
> >
> > wrote:
> >> ...
> >
> > ...
> > Allowing ref to accept rvalues goes completely against the idea
> > that ref is for passing an object so that it can be mutated and
> > have its result affect the caller. With this DIP, we'd likely
> > start seeing folks using ref all over the place even when it
> > has nothing to do with having the function mutating its
> > arguments, and that's not only error-prone, but it obfuscates
> > what ref was originally intended for.
> > ...
>
> So, if `immutable` would be the default in D, you would be okay
> with "DIP 1016"?  Because then people would have to write `ref
> mutable` for mutation

No. I don't see why that would help at all.

Honestly, if D had immutable by default, I'd probably quit D, because it
would make the language hell to use. Some things make sense as immutable but
most don't. If I wanted that kind of strait jacket, I'd use a language like
Haskell.

But regardless, even if I could put up with a such a default, it wouldn't
help, because the use case that Manu is trying to solve would be using ref
mutable just like the use cases that ref is normally used for now. There
needs to be a distinction between the case where ref is used because the
intent is to mutate the object and the case where the intent is to avoid
having to copy lvalues and mutation is acceptable but not the goal. C++
solves this by using const, since once const is used, mutation is no longer
an issue, so the refness is clearly to avoid copying, but with how
restrictive const is in D, it probably wouldn't solve much, because many use
cases couldn't use const. We really do need a mutable equivalent to C++'s
const&. But Manu's solution unnecesarily destroys the dinstinction between
ref being used as means to mutate the argument and ref being used to avoid
copying the argument. Since we can't use const for that, we really need a
new attribute.

- Jonathan M Davis



More information about the Digitalmars-d mailing list