October 07, 2017 Implementing swap for user-defined swaps | ||||
---|---|---|---|---|
| ||||
Hello, I was implement my own range type that forwards all accesses to another range. I tried to write a `swap` function so that sort etc. could be called on my range. However, I cannot get `hasSwappableElements!ARange` to evaluate to true. But, when I copy pasted the definition of `hasSwappableElements` into my module and evaluated it, it was true. I assume this is something to do with how name lookup works in D. Here's simplified code that reproduces the same problem. https://dpaste.dzfl.pl/48a420cce261 Thanks, Balagopal. |
October 07, 2017 Re: Implementing swap for user-defined swaps | ||||
---|---|---|---|---|
| ||||
Posted in reply to Balagopal Komarath | On 10/07/2017 07:55 PM, Balagopal Komarath wrote:
> I was implement my own range type that forwards all accesses to another range. I tried to write a `swap` function so that sort etc. could be called on my range. However, I cannot get `hasSwappableElements!ARange` to evaluate to true. But, when I copy pasted the definition of `hasSwappableElements` into my module and evaluated it, it was true. I assume this is something to do with how name lookup works in D. Here's simplified code that reproduces the same problem.
>
> https://dpaste.dzfl.pl/48a420cce261
You can't provide your own swap function. `sort` etc. use std.algorithm.swap, so your range has to work with that.
|
Copyright © 1999-2021 by the D Language Foundation