Jump to page: 1 2 3
Thread overview
"static" UFCS
Jun 14, 2012
Jacob Carlborg
Jun 14, 2012
Gor Gyolchanyan
Jun 14, 2012
Regan Heath
Jun 14, 2012
Gor Gyolchanyan
Jun 14, 2012
Jacob Carlborg
Jun 14, 2012
Gor Gyolchanyan
Jun 14, 2012
Jacob Carlborg
Jun 14, 2012
deadalnix
Jun 14, 2012
Regan Heath
Jun 14, 2012
Jacob Carlborg
Jun 14, 2012
deadalnix
Jun 14, 2012
Jacob Carlborg
Jun 14, 2012
Artur Skawina
Jun 15, 2012
Jacob Carlborg
Jun 14, 2012
jerro
Jun 16, 2012
Walter Bright
Jun 16, 2012
Jacob Carlborg
Jun 16, 2012
Timon Gehr
Jun 16, 2012
Andrej Mitrovic
Jun 16, 2012
Jacob Carlborg
Jun 17, 2012
Andrej Mitrovic
Jun 16, 2012
Andrej Mitrovic
June 14, 2012
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);

-- 
/Jacob Carlborg
June 14, 2012
On Thu, Jun 14, 2012 at 10:46 AM, Jacob Carlborg <doob@me.com> wrote:
> 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);
>
> --
> /Jacob Carlborg

I'd expect it to look like this:

void fooBar(Foo)(int x) {}

-- 
Bye,
Gor Gyolchanyan.
June 14, 2012
On Thu, 14 Jun 2012 10:43:43 +0100, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> wrote:

> On Thu, Jun 14, 2012 at 10:46 AM, Jacob Carlborg <doob@me.com> wrote:
>> 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);
>>
>> --
>> /Jacob Carlborg
>
> I'd expect it to look like this:
>
> void fooBar(Foo)(int x) {}

That looks too much like a template function to me.  What about:

void fooBar(static Foo, int x) {}

Note: no parameter name for the "static" Foo parameter (as it's not really a parameter - simply a placeholder to indicate it's UFCS).


C# doesn't have static UFCS (called "extension methods" in C# parlance) tho some people have wanted it:
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/8ac0e6bf-c859-4cc4-919f-c80eedfccf63

I guess the reason it doesn't exist is that there is no technical reason for it, all it gives you is a nicer syntax.

You can get fairly close with a custom static class, static method taking an instance of the class you want to 'extend'.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
June 14, 2012
On Thu, Jun 14, 2012 at 1:52 PM, Regan Heath <regan@netmail.co.nz> wrote:
> On Thu, 14 Jun 2012 10:43:43 +0100, Gor Gyolchanyan <gor.f.gyolchanyan@gmail.com> wrote:
>
>> On Thu, Jun 14, 2012 at 10:46 AM, Jacob Carlborg <doob@me.com> wrote:
>>>
>>> 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);
>>>
>>> --
>>> /Jacob Carlborg
>>
>>
>> I'd expect it to look like this:
>>
>> void fooBar(Foo)(int x) {}
>
>
> That looks too much like a template function to me.  What about:
>
> void fooBar(static Foo, int x) {}
>
> Note: no parameter name for the "static" Foo parameter (as it's not really a parameter - simply a placeholder to indicate it's UFCS).
>
>
> C# doesn't have static UFCS (called "extension methods" in C# parlance) tho
> some people have wanted it:
> http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/8ac0e6bf-c859-4cc4-919f-c80eedfccf63
>
> I guess the reason it doesn't exist is that there is no technical reason for it, all it gives you is a nicer syntax.
>
> You can get fairly close with a custom static class, static method taking an instance of the class you want to 'extend'.
>
> R
>
> --
> Using Opera's revolutionary email client: http://www.opera.com/mail/

Yes, that does look bad. How about this?

void fooBar(T : Foo)(int x) { }

-- 
Bye,
Gor Gyolchanyan.
June 14, 2012
On 2012-06-14 12:25, Gor Gyolchanyan wrote:
> Yes, that does look bad. How about this?
>
> void fooBar(T : Foo)(int x) { }

I don't like that it's a template function, there's really no reason. It seems to just be a limitation in the syntax.

-- 
/Jacob Carlborg
June 14, 2012
On Thu, Jun 14, 2012 at 3:17 PM, Jacob Carlborg <doob@me.com> wrote:
> On 2012-06-14 12:25, Gor Gyolchanyan wrote:
>>
>> Yes, that does look bad. How about this?
>>
>> void fooBar(T : Foo)(int x) { }
>
>
> I don't like that it's a template function, there's really no reason. It seems to just be a limitation in the syntax.
>
> --
> /Jacob Carlborg

It doesn't matter whether a function is a template or not. The only thing that matters is the number of instances it produces. A template function, which produces only one instance is equivalent to a non-template function. The function above can produce only one instance (unless overloaded).

-- 
Bye,
Gor Gyolchanyan.
June 14, 2012
On 14-06-2012 11:52, Regan Heath wrote:
> On Thu, 14 Jun 2012 10:43:43 +0100, Gor Gyolchanyan
> <gor.f.gyolchanyan@gmail.com> wrote:
>
>> On Thu, Jun 14, 2012 at 10:46 AM, Jacob Carlborg <doob@me.com> wrote:
>>> 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);
>>>
>>> --
>>> /Jacob Carlborg
>>
>> I'd expect it to look like this:
>>
>> void fooBar(Foo)(int x) {}
>
> That looks too much like a template function to me. What about:
>
> void fooBar(static Foo, int x) {}

static?

Oh boy, here we go again ... ;)

>
> Note: no parameter name for the "static" Foo parameter (as it's not
> really a parameter - simply a placeholder to indicate it's UFCS).
>
>
> C# doesn't have static UFCS (called "extension methods" in C# parlance)
> tho some people have wanted it:
> http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/8ac0e6bf-c859-4cc4-919f-c80eedfccf63
>
>
> I guess the reason it doesn't exist is that there is no technical reason
> for it, all it gives you is a nicer syntax.
>
> You can get fairly close with a custom static class, static method
> taking an instance of the class you want to 'extend'.
>
> R
>


-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
June 14, 2012
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.

What would be the use case for such a feature ?
June 14, 2012
On Thu, 14 Jun 2012 13:33:23 +0100, deadalnix <deadalnix@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 ?

Assuming;
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.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
June 14, 2012
On 2012-06-14 13:30, Gor Gyolchanyan wrote:

> It doesn't matter whether a function is a template or not. The only
> thing that matters is the number of instances it produces. A template
> function, which produces only one instance is equivalent to a
> non-template function. The function above can produce only one
> instance (unless overloaded).

Yeah, except when it's an instance method, which will make it non-virtual. In this case it's not though.

-- 
/Jacob Carlborg
« First   ‹ Prev
1 2 3