Extension Functions in D

383 Views Asked by At

I bought "The D Programming Language" a little while ago. Great book, very educational. However I'm having trouble trying to compile a language feature listed in the book: Extension Functions.

In the book, Andrei writes any function(a, b) can be invoked like: a.function(b); So I should be able to do this:

struct Person {
    string name;
}

void foo(Person person, string name) {
    person.name = name;
}

void main() {
    auto bob = Person();
    bob.foo("Bob Dole");  // ERROR: Person does not have method 'foo'
}

Correct? Is this feature not implemented yet, or am I just missing something? I notice that importing std.range adds methods to arrays so it does appear to be implemented at some level.

2

There are 2 best solutions below

1
On BEST ANSWER

Just wanted to state, that Uniform Function Call Syntax has been implemented.

There is a nice Dr. Dobbs article about it: Uniform Function Call Syntax on Dr. Dobbs

5
On

I take it you mean "Psuedo Members" as talked about in section 5.9.1. Currently this feature is only implemented for arrays, though this is a planned feature. In the D community you will also see it referred to as "Uniform Function Call Syntax."

Here's the bug report which will be closed when this feature is implemented: Issue 3382