sort API design

mipri mipri at minimaltype.com
Wed Nov 20 10:11:54 UTC 2019


On Wednesday, 20 November 2019 at 09:33:25 UTC, Johannes Riecken 
wrote:
> 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;
   do_something(x);

What is the point of the first assignment? Waste heat from the 
CPU?
Of course there's no point, and SPARK makes it an error to do 
that:
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 
order!1
   2. sort() discards arr0's order!1 and then provides a new 
order!2
   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;
   do_something(o);

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



More information about the Digitalmars-d mailing list