Comparison of two 'dynamic arrays'.

matheus. matheus at gmail.com
Sun Nov 13 17:53:52 UTC 2022


On Sunday, 13 November 2022 at 17:10:23 UTC, DLearner wrote:
> ...
> The slight generalisation shown at bottom also worked.
>
> However, is there a way of avoiding the for-loop?
> ...

I don't have too much knowledge in D, but I think so. (My main 
language is C).

Well, one way to make things "better" would be creating a 
constructor inside that struct with opCall, something like this:

import std.stdio;

void main() {
    struct test_struct {
       char[] Txt;
       static test_struct[] opCall(test_struct[] e){
         auto _ = e.dup;
         foreach(i,v; e[0].Txt){
             _[0].Txt = e[0].Txt.dup;
         }
         return _;
     }
    }
    test_struct[] A;
    A.length = 1;
    A[0].Txt.length = 1;
    A[0].Txt[0] = 'X';
    writeln(A);
    auto B = test_struct(A);
    writeln(A, B);
    A[0].Txt[0] = 'Y';
    writeln(A, B);
}

There still a loop over there, so let's wait for some advanced 
users to destroy this. :)

Matheus.


More information about the Digitalmars-d-learn mailing list