"static" UFCS

Regan Heath regan at netmail.co.nz
Thu Jun 14 06:57:30 PDT 2012

On Thu, 14 Jun 2012 13:33:23 +0100, deadalnix <deadalnix at gmail.com> wrote:

> Le 14/06/2012 08:46, Jacob Carlborg a écrit :
>> UFCS can be used to emulate adding new members/instance methods to a
>> class or struct:
>> class Foo
>> {
>> }
>> void bar (Foo foo, int x) {}
>> auto foo = new Foo;
>> foo.bar(3);
>> Is it possible, somehow, to emulate adding new _static_ methods to a
>> class, something like this:
>> void fooBar (/*something*/, int x) {}
>> Making this possible:
>> Foo.fooBar(4);
> I already think that static method was a bad idea from the « everything  
> have to be an object » time.
> The need for static function is pretty weak when we have free function  
> and that they can access private objects data as needed.

Good point.  A module level free function in D is essentially a static  
class method for /all/ classes in the module.  I think people like static  
methods over free functions for aesthetic/organisational reasons, not for  
functional ones.  Except.. if it's a static method then as it's called  
with syntax like <class>.<method> it cannot collide with a free function  
called <method>.  So, perhaps it helps with function lookup and  
collisions, much like namespaces do.

> What would be the use case for such a feature ?

1. You have no control over the class Foo, nor it's module
2. You don't want private or protected access to Foo's members

Then all you'd get with static UFCS is nicer calling syntax, and possibly  
less lookup/collisions, that's it really.


Using Opera's revolutionary email client: http://www.opera.com/mail/

More information about the Digitalmars-d mailing list