DMD 0.170 release

Bill Baxter dnewsgroup at billbaxter.com
Tue Oct 17 20:11:41 PDT 2006


Tom S wrote:
> Bill Baxter wrote:
>> Tom S wrote:
>>> Bill Baxter wrote:
>>>> Bill Baxter wrote:
  ----
>>> import std.stdio;
>>>
>>>
>>> struct reverse__(AggregType) {
>>>     alias typeof(AggregType[0]) ElemType;
>>>     AggregType arr;
>>>     int opApply(int delegate(inout ElemType) dg) {
>>>         int ret = 0;
>>>         for (int i = arr.length -1; i >= 0; --i){
>>>             ret = dg(arr[i]);
>>>             if (ret) break;
>>>         }
>>>         return ret;
>>>     }
>>> }
>>>
>>> reverse__!(T) reverse(T)(T x) {
>>>     reverse__!(T) res;
>>>     res.arr = x;
>>>     return res;
>>> }
>>>
>>>
>>> void main() {
>>>     char[] foo = "foo bar";
>>>     foreach (c; foo.reverse) {
>>>         writefln(c);
>>>     }
>>> }
>>
>> Did you mean
>>    foreach (c; reverse(foo)) ??
>> I guess so.  it does seem to compile that way.
> 
> Actually I meant foo.reverse. It compiles and runs fine on .169

Ok, sure it compiles, but if you're just going to reverse the array 
in-place, then you don't need all that reverse__ stuff up there.  I'm 
confused... :-?   But anyway the reverse__ stuff *is* what I was looking 
to do and it does compile, too, and it does manage to iterate over the 
array without modifying it.

>> I think this falls into Walter's "dummy classes and hackish stuff" 
>> category though.
> 
> It's not that bad. There's just one small dummy struct that resides on 
> the stack. The rest is a function that returns it and a generic 
> implementation of 'reverse' that will work on any array type.

Any array type, or anything that has a .length property and overloads 
opIndex, right?

>> Is there no way to make something similar work with a nested function? 
>> If my reversed function above could just get the pointer to the actual 
>> array then it seems like it should work.  Is there some way to declare 
>> the AggregateT array parameter so that that happens?
> 
> 
> Not without dummy classes and hackish stuff <g>

Seriously?  Is there no way to write the function below so that the 
program prints out the same value twice?

void print_addr( ??? arg )
{
    writefln(&arg);
}

void main()
{
     int[] arr = [1,2,3,4,5];
     writefln(&arr);
     print_addr(arr);
}

--bb



More information about the Digitalmars-d-announce mailing list