sort API design

mipri mipri at
Wed Nov 20 10:11:54 UTC 2019

On Wednesday, 20 November 2019 at 09:33:25 UTC, Johannes Riecken 
> Or are there
> any ideas in programming language research that would help with 
> such a case?

I remember reading that Ada/SPARK did some checking that could've
helped here, but I can't find a reference now, and I'm not sure of
the terminology. It might be one of the implications of the data
flow analysis that it does.

Suppose you had this in your code:

   int x;
   x = 2;
   x = 3;

What is the point of the first assignment? Waste heat from the 
Of course there's no point, and SPARK makes it an error to do 
your program is doing some *work*, it is generating some outputs,
so it must go on to use those outputs, and not throw them away.

And in your program that led to a confusing result, you're also
performing work that is lost:

>     auto case_sensitive   = sort!((a, b) => a           < b
>    )(arr0);
>     auto case_insensitive = sort!((a, b) => a.toLower() <
> b.toLower())(arr0);

That first sort() has a return value, but a conceptual output is
the order that it provides to arr0, and the second sort() can be
statically known to destroy order in an arr0 provided to it. So:

   1. sort() discards arr0's order!0 and then provides a new 
   2. sort() discards arr0's order!1 and then provides a new 
   3. writeln reads order!2
   4. writeln reads order!2

Or, written like the first example above:

   Order o;
   o = order!1;
   o = order!2;

What is the point of the first assignment? Waste heat from the 

More information about the Digitalmars-d mailing list