sort API design

Johannes Riecken johannes.riecken at
Wed Nov 20 09:33:25 UTC 2019

I had looked at how to do case-insensitive string sorting with 
Phobos and without looking at the documentation much, I went with 
the following code and then wrongly concluded that `<` does 
case-insensitive comparisons on strings by default:

import std.algorithm;
import std.stdio;
import std.string;

void main() {
     auto arr0 = ["foo", "bar", "Baz"];
     auto case_sensitive   = sort!((a, b) => a           < b       
     auto case_insensitive = sort!((a, b) => a.toLower() < 
["bar", "Baz", "foo"]
["bar", "Baz", "foo"]

Later I learned that I was trapped by `sort` being in-place but 
still returning a value. If this API were to be designed from 
scratch, I wonder if it would be possible to rule out this 
incorrect usage at compile-time while still retaining the other 
nice features of the `sort` function like being in-place and 
giving strong types which can be used to choose faster algorithms 
to further transform the result, etc.? Or are there any ideas in 
programming language research that would help with such a case?

More information about the Digitalmars-d mailing list