www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24005] New: UFCS: The non-member function call can be

https://issues.dlang.org/show_bug.cgi?id=24005

          Issue ID: 24005
           Summary: UFCS: The non-member function call can be
                    uniform/universal, too
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

UFCS makes it possible to call a non-member function as if it were a member
function of the first parameter’s type. The reverse is not true, however. A
member function cannot be called as if it were a non-member function that takes
the aggregate type that contains it as the first parameter explicitly:
```d
struct S
{
    void mem() {}
}
void nonmem(S) {}

void main()
{
    S s;
    s.mem();    // ok
    mem(s);     // fails
    s.nonmem(); // ok (UFCS)
    nonmem(s);  // ok
}
```

I suggest to make the non-member call syntax universal as well. The same as
preferring the member function in member function syntax over an eponymous
non-member function in scope, we prefer the non-member function in the
non-member function call syntax over an eponymous member function of the type
of the first argument.

This, plus https://issues.dlang.org/show_bug.cgi?id=24004, would make it
unnecessary to remember if a functionality is provided via a non-member or
member function in quite a lot of cases.

A bit of nomenclature:
UNFCS: Universal non-member function call syntax. The (new) mechanic that
`mem(obj)` works because `obj` has a member `mem`.
UMFCS: Universal member function call syntax: The (already present) mechanic
that `obj.nonmem` works because a non-member function takes `typeof(obj)` as
its first parameter.
UFCS: Umbrella term for both UNFCS and UMFCS.

--
Jun 21 2023