D 中的扩展函数

发布于 2024-12-09 19:39:34 字数 489 浏览 11 评论 0原文

我不久前买了《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳钉梦 2024-12-16 19:39:34

我认为您指的是第 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

玩心态 2024-12-16 19:39:34

只是想声明,统一函数调用语法已经实现。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文