Uniform Function Call Syntax(UFCS) and @property

kenji hara k.hara.pg at gmail.com
Mon Mar 7 10:28:26 PST 2011


> The problem is when there is a conflict between an actual member funtion,
> and a free function which takes the type as its first argument.
>
> Who wins?  The obvious choice is the member function.  But let's say the
> member function is added to the type long after the external function
> exists.  People who have written code that uses the external function via
> UFCS all of a sudden are now calling the member function, without warning.
>  This is a form of hijacking.

Why do you prefer module functions? It will be selected by UFCS only
when there is no member that has the same name in class/struct. It is
clear.

> You could say an ambiguous case should be an error, but then someone is able
> to issue a sort of "denial of service" attack by simply defining a free
> function which coincides with a member function.

I don't understand the meaning of "denial of service" what you say. Is
it causing compile error? It is same behavior with overload set
conflict.

> With builtins, there is no conflict.  You cannot define member functions for
> integers or for arrays, so the ambiguity is gone.

Almost yes. (Builtin array has 'length' property. It may  conflict
module function 'length'.)

> And let's also not forget the WTF factor of when you read code that uses a
> UFCS function, where would be the first place you look for it?  In the class
> or struct.

Probably, but it seems to me that it is the software design problem.

> I see almost no benefit for having UFCS on classes and structs.  It is
> already possible to add member functions to those types, and the difference
> between x(y) and y.x() is extremely trivial.  If anything, this makes member
> functions lose their specificity -- a member function is no longer
> attributed to the struct or class, it can be defined anywhere.

UFCS is veri usuful. It reduce fat interfaces of class/struct, and
separate 'useful but surrounded operation' from core operations of the
object.

A simple example is std.stdio.File.writefln. This function does the
almost same job std.stdio.writefln. Correct UFCS will cut wastes like
this.

Kenji


More information about the Digitalmars-d mailing list