假设我已经导入了 std.algorithm.BinaryHeap
,并想调用其 removeAny
其他方法(例如, delete_min
)。如果我从 std.algorithm
导入方法本身,我可以写这样的东西:
import std.algorithm: removeAny;
alias delete_min = removeAny;
但是,我显然不能这样做,因为
removeAny
是BinaryHeap
的方法.我怎样才能将它别名为其他东西?
最佳答案
我认为最好的(如果不是唯一的)方法是定义一个简短的扩展方法:
auto delete_min(T...)(ref BinaryHeap _this, T other_args_here) {
return _this.removeAny(other_args_here);
}
然后你可以把它称为
yourthing.delete_min(other_args)
并且编译器应该内联它删除另一个小层。
关于D 中的别名对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701172/