Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension Functions in D

Tags:

d

dmd

gdc

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.

like image 727
F i L Avatar asked Oct 14 '11 21:10

F i L


2 Answers

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

like image 163
Justin W Avatar answered Oct 05 '22 20:10

Justin W


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

like image 28
RedX Avatar answered Oct 05 '22 19:10

RedX