std.allocator ready for some abuse

Brad Anderson eco at gnuk.net
Sat Oct 26 11:42:41 PDT 2013


On Saturday, 26 October 2013 at 16:10:46 UTC, Dmitry Olshansky 
wrote:
> 26-Oct-2013 20:01, Joseph Rushton Wakeling пишет:
>> On 25/10/13 20:41, Namespace wrote:
>>> Did you mean to get rid of built-in arrays / kill int[] and 
>>> replace it
>>> with
>>> Array!T?
>>
>> Array!T is problematic as things stand -- e.g. you can't 
>> foreach over
>> one.
>
> Sure you can. Try it and rejoice:
>
> void main()
> {
>     import std.container, std.stdio;
>     Array!int a = make!(Array!int)(1,2,3,4);
>     //the rule is: if a can be sliced then slice it and use 
> that slice
>     foreach(v; a)
>     {
>         writeln(v);
>     }
> }
>

The fact that foreach with a ref item or foreach with an index 
doesn't work makes using them a lot more of a hassle than 
built-in arrays though.

     void main()
     {
         import std.container, std.stdio;
         Array!int a = make!(Array!int)(1,2,3,4);
         foreach(ref v; a)
         {
             v *= v; // no effect on a
         }

         writeln(a[]); // [1, 2, 3, 4]

         foreach(v, i; a) // Error: cannot infer argument types
         {
             a[i] *= v;
         }
     }



More information about the Digitalmars-d mailing list