12

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.

F i L
  • 760
  • 3
  • 12

2 Answers2

12

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

Justin W
  • 2,077
  • 12
  • 17
  • There are a number of issues which must be sorted out before UFCS can be implemented for anything other than arrays, and because of that it may never actually be implemented, though there's definitely a lot of folks that want it to make it into the language in some form or another at some point. So, there's a decent chance that it'll happen at some point, but it's not a sure thing. But having it for arrays is definitely a great feature. – Jonathan M Davis Oct 15 '11 at 11:01
  • At least it will complicate member lookup enormously since it has to interact with alias this and the like. – Trass3r Oct 15 '11 at 12:21
  • 3
    I don't see the point of UFCS to be honest. It is purely syntax sugar, but complicates name lookup massively. Is it really that much of an issue to say `length(a)` instead of `a.length`? – Peter Alexander Oct 16 '11 at 14:35
  • 3
    One of the main benefits is that it makes function chaining easy. So instead of `join(map!"fun"(split(array, ",")))` you write `split(array, ",").map!"fun"().join()`. You can create long chains like this and use newlines to make it look nice and clean. – Justin W Oct 17 '11 at 16:04
  • 2
    Another advantage is that you can "contribute" to the interface a 3rd party type provides in order to make it usable by a 4th party template. – BCS Oct 18 '11 at 20:14
2

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

RedX
  • 14,749
  • 1
  • 53
  • 76
  • Yep! Definitely knew about this already, but thanks for commenting. I've marked this as the answer now, so that others will know as well. – F i L Apr 16 '12 at 17:13