A quick note on trailing delegates and type inference.

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Sun Oct 29 22:03:56 PST 2006


Here's just something which had been nagging at me for a while but I 
forgot to say: if Walter decides to implement trailing delegates as 
*the* way to do iteration (and I can't see why not, since they are by 
far the most general solution) then I ask that iterating over arrays be 
slightly cleaned up, so that we can use type inference on *all* arrays. 
Currently, we have this problem (unless I'm missing something):

   int[] ifoo = [1,2,3];
   foreach (i; ifoo) {...} // Fine

   long[] lfoo = [1,2,3];
   foreach (i; lfoo) {...} // Fine

   char[] cfoo = "abc";
   foreach (c; cfoo) {...} // Doesn't compile -- can't infer type of c.

I appreciate the reason for multiple forms of iteration over char[]s, 
but this limitation is annoying and breaks genericity (since mostly you 
can assume that the type is inferred and you don't have to worry about 
the exact type, but with (w/d)char[] types, you do).

There's a simple solution, if trailing delegates are adopted: make (say) 
'each' the default iterator for arrays, and it has only one overload, so 
that the type is always inferred. Then, for char[] types, which need 
extra overloads, name the iterator differently, eg 'eachUTF8/16/32', 
'eachDChar', etc.

Please do this, since type inference is a powerful tool and this is an 
easy problem to fix.

Cheers,

Reiner



More information about the Digitalmars-d mailing list