Search code examples
aliasd

Aliasing object methods in D


Suppose I've imported std.algorithm.BinaryHeap, and want to call its removeAny method something else (for instance, delete_min). If I were importing the method from std.algorithm itself, I could write something like this:

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

However, I obviously cannot do that, as removeAny is a method of BinaryHeap. How can I alias it to something else?


Solution

  • I think the best, if not the only way to do this is to define a short extension method:

    auto delete_min(T...)(ref BinaryHeap _this, T other_args_here) {
        return _this.removeAny(other_args_here);
    }
    

    Then you can call that as yourthing.delete_min(other_args) and the compiler ought to inline it removing the other little layer.