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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jul 20 19:51:19 UTC 2018


On Friday, July 20, 2018 12:21:42 Manu via Digitalmars-d wrote:
> I think disabling it is going to be the overwhelmingly niche case, but
> it's good that you can still do it and be satisfied if that's what you
> want to do.
>
> Can you link me to a single line of your own code where you've used
> this pattern?

What, having ref mutate its argument and that being its entire purpose for
being there? How about almost every time I have ever used ref ever. e.g.
this is exactly what some functions in dxml do, and it would be just plain
wrong to pass an rvalue - e.g. the overload of parseDOM that operates on an
EntityRange:

http://jmdavisprog.com/docs/dxml/0.3.2/dxml_dom.html#.parseDOM

or a function like writeTaggedText

http://jmdavisprog.com/docs/dxml/0.3.2/dxml_writer.html#.writeTaggedText

which writes to the XMLWriter that it's given. Having either of those accept
rvalues would just cause bugs.

In the few cases where it seems appropriate to accept both rvalues and
lvalues without copying either, I can almost always use auto ref, because I
rarely use classes. I agree that being able to accept lvalues without
copying them would be useful for non-templated functions as well, but I do
not at all agree that that is how ref should work in general. IMHO, as with
auto ref, it really should be marked with an attribute to indicate that it
is purposefully accepting rvalues. I would not consider it a niche case at
all for ref to not accept rvalues.

> > Either way, what this DIP proposes means that existing uses of ref will
> > need to be changed - and in an extremely verbose way - in order to get
> > back their current behavior.
>
> Their currently behaviour is mostly wrong, and the exact thing I'm
> trying to fix though.

Why would the current behviour be mostly wrong? If the purpose of using ref
is to accept the current value of a variable and mutate it such that the
argument is mutated, then the current behaviour is _exactly_ what's
desirable, and it prevents bugs.

- Jonathan M Davis



More information about the Digitalmars-d mailing list