www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implementing swap for user-defined swaps

reply Balagopal Komarath <baluks gmail.com> writes:
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.
Oct 07 2017
parent ag0aep6g <anonymous example.com> writes:
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.
Oct 07 2017