D mentioned on Rust discussions site

Timon Gehr timon.gehr at gmx.ch
Sat May 23 18:46:40 UTC 2020


On 23.05.20 19:08, Andrei Alexandrescu wrote:
> On 5/23/20 9:18 AM, Dibyendu Majumdar wrote:
>> On Saturday, 23 May 2020 at 02:37:23 UTC, Walter Bright wrote:
>>> C missed some obviously great ideas. One is nested functions, which 
>>> fit right in with C's semantics. The other is the fix for:
>>>
>>> https://www.digitalmars.com/articles/C-biggest-mistake.html
>>
>> I do not think it was a mistake at all. Treating pointers and arrays 
>> uniformly is one of the great innovations in C.
>>
>> https://www.lysator.liu.se/c/bwk-on-pascal.html
> 
> The article does not support the argument. The article (section 2.1) 
> discusses statically-sized arrays only, and the problem is that the size 
> is part of the type; however, that's the case in C as well, as it should.
> 
> There's no doubt in my mind that the absence of a universal "doped 
> pointer" (pointer plus extent) built-in or standard library in C has 
> caused innumerable problems. The fact that early C versions could not 
> return structs by value might have contributed to that decision.

I think they also considered it wasteful because lengths would often be 
stored multiple times, for example if you have multiple arrays of the 
same length or otherwise related lengths.

Fixed-size arrays where the length is part of the type can actually work 
well, the type system just has to be a little less naive. This is what I 
do in my research programming languages.

For example, the following merge sort code uses fixed-size arrays, where 
the sizes are run-time values:

def merge[a:*](less: a×a→𝔹)[n:ℕ,m:ℕ](xs: a^n, ys: a^m): a^(n+m){
     (i, j) := (0, 0);
     r := []: a[];
     while i<n ∧ j<m{
         if less(xs[i], ys[j]){
             r ~= [xs[i]];
             i += 1;
         }else{
             r ~= [ys[j]];
             j += 1;
         }
     }
     return r ~ xs[i..n] ~ ys[j..m] coerce a^(n+m);
}
def sort[a:*](less: a×a→𝔹)[n:ℕ](xs: a^n): a^n{
     if n<=1{ return xs; }
     rec := sort(less);
     return merge(less)(rec(xs[0..n div 2]), rec(xs[n div 2..n]));
}


More information about the Digitalmars-d mailing list