我创建了一个实现Atkin筛选以找到素数的类.该类存储结果并提供"isPrime"方法.我还想添加一个范围,允许您迭代质数.我在考虑这样的事情:
@property auto iter() { return filter!(this.isPrime)(iota(2, max, 1)); }
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用:
Error: function primes.primes.isPrime (ulong i) is not callable using argument types ()
Error: expected 1 function arguments, not 0
Run Code Online (Sandbox Code Playgroud)
没有"这个"我得到了
Error: this for isPrime needs to be type primes not type Result
Run Code Online (Sandbox Code Playgroud)
有没有办法将成员函数作为模板参数传递?
您不能将方法(委托)用于模板参数,因为它们需要一个在编译时不知道的上下文.
您可以创建isPrime静态方法或自由函数(然后删除this.并且您的代码将起作用),或者(如果方法不是有意的静态),请使用匿名委托文字:
@property auto iter() { return filter!((x) { return isPrime(x); })(iota(2, max, 1)); }
Run Code Online (Sandbox Code Playgroud)
在2.058你将能够写:
@property auto iter() { return filter!(x => isPrime(x))(iota(2, max, 1)); }
Run Code Online (Sandbox Code Playgroud)