What's the right way for doing this in D?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Aug 24 05:26:16 PDT 2013


On 8/24/13, Namespace <rswhite4 at googlemail.com> wrote:
> Until now I used Method A, but because of UFCS and the fact, that
> the dot product doesn't affect the object, I thought about which
> way would make more sense. I still like Method A but I'm curious
> what you think.

Note also that UFCS can introduce the function hijacking protection.
For example, if you have:

-----
module a;
import b;

struct A { }
void test(A a) { }

void main()
{
    B b;
    b.test();
}
-----

-----
module b;
struct B { }
void test(B b) { }
-----

This will result in:
a.d(10): Error: function a.test (A a) is not callable using argument types (B)

I tend to use UFCS when I need the function to handle multiple types -
e.g. a templated function that should be used with UFCS, or when I
want to add UFCS syntax to a type which I have no control over (e.g. a
Phobos or 3rd party library type).

Otherwise if your function is only ever going to be used with a single
type you might as well put it as a member function, unless you prefer
otherwise (e.g. some people dislike indentation).

Btw, I thought UFCS might disable using the 'super' syntax, but to my
surprise it actually works:

-----
class A
{
}

void foo(A a) { }

class B : A
{
    void test()
    {
        super.foo();  // ok
    }
}

void main()
{
}
-----

Pretty cool.

Finally a note about the documentation. If you put your functions as
member functions, the documentation becomes part of the aggregate type
that they're members of. So it might make the documentation easier to
read.


More information about the Digitalmars-d-learn mailing list