Methods for merging objects in D

Suppose I have imported std.algorithm.BinaryHeapand want to call its method removeAnysomething else (for example, delete_min). If I imported a method from std.algorithmmyself, I could write something like this:

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

However, I obviously cannot do this because it removeAnyis a method BinaryHeap. How can I use it somehow else?

+4
source share
1 answer

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 name it as yourthing.delete_min(other_args), and the compiler should install it by deleting another small layer.

+6
source

Source: https://habr.com/ru/post/1533816/


All Articles