D 中的扩展函数
我不久前买了《The D 编程语言》。很棒的书,很有教育意义。然而,我在尝试编译书中列出的语言功能时遇到了麻烦:扩展函数。
在书中,Andrei 写道任何函数(a,b)都可以像这样调用:a.function(b);所以我应该能够做到这一点:
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'
}
对吗?这个功能还没有实现吗,或者我只是错过了一些东西?我注意到导入 std.range 向数组添加了方法,因此它似乎确实在某种程度上实现了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您指的是第 5.9.1 节中讨论的“伪成员”。目前,此功能仅针对阵列实现,尽管这是一个计划中的功能。在 D 社区中,您还会看到它被称为“统一函数调用语法”。
以下是实施此功能后将关闭的错误报告:问题 3382
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
只是想声明,统一函数调用语法已经实现。
Dobbs 博士有一篇关于它的好文章:Dr. Dobbs 上的统一函数调用语法
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