非会员范围功能 [英] Non Member range functions

查看:72
本文介绍了非会员范围功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要为其实现范围的类。我想以phobos库的方式(即在主类之外)实现这些功能。

I have a class that I'm implementing ranges for. I'd like to implement the functions the way the phobos library does, i.e. outside the main class.

void popBack(T)(ref T[] a) if (!is(Unqual!T == char) && !is(Unqual!T == wchar))
{
    assert(a.length);
    a = a[0 .. $ - 1];
}

这是我的版本:

void popFront(T)(ref PersistentList!(T) a)
{
    a = a.next();   
}

当我尝试使用Forech编译此代码时,我得到:

When I try to compile this code with a forech, I get:

Error   1   Error: no property 'popFront' for type 'stmd.PersistentList!(int).PersistentList'   main.d  

我可以将成员代码移到主类中,但是由于我正在修改输入的 ref值,不能使用popFront()我真的需要popFront(ref a)。

I could move the member code into the main class, but since I'm modifying the input "ref" value I can't use popFront() I really need popFront(ref a).

我在做什么错了?

推荐答案

你在做什么尝试执行的操作取决于D社区调用统一函数调用语法的方式。这种功能可以使用与成员函数相同的语法来调用非成员函数。这对于数组仅实现了很长时间,关于它走多远有一些争论。已经决定应该对所有类型都实现它,但是编译器实现者Walter Bright尚未做到这一点,因为他一直在热衷于64位支持。

What you're trying to do depends on what the D community calls uniform function call syntax. This is the ability to call non-member functions with the same syntax as member functions. This has been implemented for arrays only for a long time, and there was some debate about how far to take it. It's been decided that it should be implemented for all types, but Walter Bright, the compiler implementer hasn't gotten around to doing this yet because he's been working feverishly on 64-bit support.

统一函数调用语法在Andrei Alexandrescu的书中描述, D编程语言,它被认为是D2的权威指南。它当前缺乏实现,被认为是一个错误。此问题应该在几个版本中得到解决。

Uniform function call syntax is described in Andrei Alexandrescu's book, "The D Programming Language", which is considered the authoritative guide to D2. Its currently lack of implementation is considered a bug. This should be fixed in a few releases.

这篇关于非会员范围功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆