The liabilities of binding rvalues to ref

Jonathan M Davis jmdavisProg at gmx.com
Thu May 9 21:44:08 PDT 2013


On Thursday, May 09, 2013 22:33:09 Steven Schveighoffer wrote:
> On Thu, 09 May 2013 22:10:22 -0400, Jonathan M Davis <jmdavisProg at gmx.com>
> > That would be great except for UFCS. How would you designate the ref
> > when it's
> > the first argument? And I think that it's worse to have ref optional at
> > the
> > callsite than to not have it at all.
> 
> 1. using UFCS is optional.  c[i].fix() works as well as fix(c[i]).

That would be an option, but as I explain belong, I think that having ref be 
optional at the call site is a bad idea.

> 2. We could say UFCS has an implicit ref-at-callsite if needed, and will
> not compile for rvalues.  If you think about it, 'this' is implicitly ref,
> and UFCS is calling like the first argument is the 'this' reference.

That would make using UFCS with ranges generally illegal, as most range-based 
funtions do not have ref parameters, and you generally don't want them to be 
passed by ref.

> > If it weren't for UFCS, I probably would be in favor of requiring it at
> > the
> > callsite, but I just don't see how that could work with UFCS. Maybe C#
> > has a
> > way to deal with that, since it does have some sort of UFCS, and it does
> > require ref at the callsite (at least from what I understand - I haven't
> > used
> > C# much)?
> 
> C# required ref when I used it.  It was actually kind of nice because it
> was self-documenting.
> 
> But I don't think it's something we could add as a requirement, too much
> code uses ref.
> 
> I don't think it is bad to make it optional.

This has come up on the newsgroup before, and I really do think that having it 
as optional is worse than not having it. If it's required, then when you see 
that an argument is marked with ref, you know that the parameter is a ref 
parameter, and when you see an argument without ref, you know that the 
parameter is not a ref parameter. And the compiler enforces both, so you 
always catch it when the function changes.

On the other hand, if ref at the callsite is optional, then the lack of ref at 
the call site means absolutely nothing. You're still going to have to look at 
every single function signature to determine if it takes a ref argument or 
not, meaning that the lack of ref at the call site buys you nothing at best 
and harms you at worst, because it gives you a false sense of security that 
the argument is not being passed by ref. Programmers would have to be 100% 
consistent in using ref at the callsite to avoid that, and that's obviously 
not going to happen.

You _do_ still get the gain that if you use ref at the call site, then your 
code will break if the parameter is no longer ref. But I think that the fact 
that the lack of ref means nothing while still giving the impression that the 
parameter is not a ref parameter is worse than never having ref at the call 
site.

- Jonathan M Davis


More information about the Digitalmars-d mailing list