QuickSort on ranges

Steven Schveighoffer schveiguy at gmail.com
Mon Oct 5 02:41:42 UTC 2020

On 10/4/20 6:50 AM, jerome wrote:
> Thanks you very much Ali,
> I will try to wrap my head around your inputs, and get a better 
> understanding of ranges in the process. I feel there is a lot of power 
> in D ranges, and like the hammer of Thor, I am not worthy yet :)

Just to elaborate a bit further: Templates work by pattern matching. 
When you say

quickSort(T)(T[] r)

You are saying, match the parameter T[] to what is passed, and if it's a 
match, set T to the part of the parameter that matches. Therefore:

int[] => T = int
ubyte[] => T = ubyte

But if you leave off the array brackets:

quickSort(T)(T r)

Now T matches *any type* that you pass in, including ranges. Most range 
functions use templates because there is no supertype for ranges. They 
are checked by trying to compile the range primitives on them.

How you do more advanced checks other than pattern matching, is to use a 
template constraint, as Ali suggests.

In fact, since you are just using std.algorithm.pivotPartition, you can 
just look at the constraints it uses:

if (isRandomAccessRange!Range && hasLength!Range && hasSlicing!Range && 


More information about the Digitalmars-d-learn mailing list