Extension Functions in D
Asked Answered
I

2

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.

Immunotherapy answered 14/10, 2011 at 21:24 Comment(0)
G
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

Gwenn answered 4/4, 2012 at 8:27 Comment(1)
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.Immunotherapy
A
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

Aprilaprile answered 14/10, 2011 at 21:42 Comment(5)
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.Kokaras
At least it will complicate member lookup enormously since it has to interact with alias this and the like.Effervesce
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?Yarn
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.Aprilaprile
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.Crossed
G
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

Gwenn answered 4/4, 2012 at 8:27 Comment(1)
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.Immunotherapy

© 2022 - 2024 — McMap. All rights reserved.