D 中的别名对象方法

标签 alias d

假设我已经导入了 std.algorithm.BinaryHeap ,并想调用其 removeAny其他方法(例如, delete_min )。如果我从 std.algorithm 导入方法本身,我可以写这样的东西:

import std.algorithm: removeAny;
alias delete_min = removeAny;

但是,我显然不能这样做,因为 removeAnyBinaryHeap的方法.我怎样才能将它别名为其他东西?

最佳答案

我认为最好的(如果不是唯一的)方法是定义一个简短的扩展方法:

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/

相关文章:

C# 变量名别名

c - 有没有一种方法可以快速确定从(稀疏)文件中读取的 block 是否全为零?

terminology - D 的 "static if"是声明性的还是程序性的?

parameters - "in"D 存储类

annotations - D语言中如何使用User Defined Attribute?

associative-array - 从关联数组中删除任何元素

sql - 在 WHERE 子句中使用别名

bash - 如何将 'gvim filename' 别名为 'gvim filename &'

c++ - 模板别名难度

angularjs - 为什么 Angular 中的 ng-controller 使用别名?