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

Marco Leise Marco.Leise at gmx.de
Sun Jul 22 21:33:37 UTC 2018


Am Fri, 20 Jul 2018 10:33:56 -0600
schrieb Jonathan M Davis <newsgroup.d at jmdavisprog.com>:

> 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

I understand the distinction you make, but I don't feel
strongly about it. Vector math can use const in D just fine as
there are no indirections involved, so my functions would have
`ref const` arguments all over the place to discriminate
between "for performance" and "for mutation". That said, I'm
not opposed to some other keyword or @tribute either.

-- 
Marco



More information about the Digitalmars-d mailing list