pointers, functions, and uniform call syntax

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 4 11:03:25 PDT 2012


On Tuesday, September 04, 2012 12:51:53 Artur Skawina wrote:
> On 09/03/12 20:45, Jonathan M Davis wrote:
> > It's a perfectly valid enhancement request to want
> > 
> > void func(S s, int i) {...}
> > 
> > to be be callable with S*, given that normally function calls on an S*
> > don't require you to dereference anything.
> 
> No, it's not. See http://d.puremagic.com/issues/show_bug.cgi?id=8490 for why
> this would be a very bad idea.

I completely disagree with that assessment. You already get copies with UFCS 
all over the place. It's just that if you could use a function which took the 
struct as a value with a pointer to the struct with UFCS, then you'd get a 
copy whereas if it's taking it by pointer, you wouldn't.

> However 'void func(ref S s, ...){}' should
> (be made to) work, at least for the UFCS case - if it already doesn't (old
> compiler here...) 

I completely disagree with this as well. If using UFCS with an S* and a 
function which takes an S works, then it should work with a function which 
takes ref S, but pointers are _not_ the same as ref at all, and I completely 
disagree with anything which try and make pointers convert to ref in the 
general case. It only makes sense in this particular case, because of how 
calling member functions on pointers to struct works, and in that case, the 
ref is irrelevant IMHO. Having functions take ref is _annoying_, because you 
can't pass rvalues to them, and so ref should be used sparingly.

I don't see any real difference between having a function which takes an S 
being used with UFCS and having that same function used with an S*. A copy 
occurs in both cases. It's just that with S*, it means that the compiler has 
to implicitly dereference it for you (as it already does when accessing the 
struct's members). Other than that, the semantics are identical.

> - if 'func' is supposed to emulate a method then it
> should behave like one.

Which is exactly the point of this enhancement request. If you call a member 
function on a struct pointer, you don't need to dereference anything or really 
care that it's a pointer, but with UFCS, all of a sudden you do, which breaks 
the abstraction that UFCS is trying to provide.

- Jonathan M Davis


More information about the Digitalmars-d mailing list