If I had my way

F i L witte2008 at gmail.com
Mon Dec 12 20:13:29 PST 2011


On Monday, 12 December 2011 at 21:57:36 UTC, Jonathan M Davis 
wrote:
> There has been discussion of UFCS (universal function call 
> syntax), which would then expand that beyond arrays to _all_ 
> types - int, float, any user-
> defined type, etc. However, there are a number of issues with 
> that (particularly with user-defined types; e.g. what if a 
> member function and a free function have the same name?), and 
> it has yet to be implemented. It may never be implemented.

C# easily avoids member confusion with pseudo methods by using 
the 'this' keyword as a parameter attribute in the pseudo method 
(http://msdn.microsoft.com/en-us/library/bb383977.aspx). Any 
function(this a, b) can be invoked as: a.function(b) in C#. I 
understand using the 'this' keyword as a parameter attribute 
already serves a different purpose in D, but I'm sure another 
keyword ('pseudo' perhaps?) or equivalent syntax could be 
reached. At which point, any external pseudo method conflicting 
with an objects internal members could simply result in a 
compiler error (or, member functions could take priority over 
pseudo methods).

I understand this isn't exactly a priority, but IMO it could 
really help the overall syntax feel more consistent in some areas.

    import std.conv;

    void main()
    {
        string encoded = "1324.03";
        double actual = encoded.to!string; // similar to 
Object.toString()
    }

More importantly, pseudo methods could be used to bend the rules 
of member access, and to implement module-specific functionality 
for imported objects without breaking OOP syntax.

    module a;

    class A {
        int data;
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    module b;

    pseudo void foo(A a, B b) {
        // A can access B's private members
        a.data = b._data;
    }

    class B {
        private int _data;
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    import a, b;

    void main() {
        auto a = new A();
        auto b = new B();

        a.foo(b); // cool!
    }


> So, I think that there's a decent chance that you just 
> misunderstood what that section in TDPL was really saying (it 
> really should be clearer on whether it meant just arrays or all 
> types), but there are definitely folks who want it to be 
> extended to all types, so it may be eventually, but we'll have 
> to wait and see.

Undoubtedly a misunderstanding on my end. Still, +1 for seeing 
Pseudo Members for all types.

- Philip




More information about the Digitalmars-d mailing list