Thread overview
Using .require for struct types
Sep 10, 2022
Erdem Demir
Sep 10, 2022
Ali Çehreli
Sep 13, 2022
Erdem
Sep 13, 2022
Erdem
Sep 11, 2022
Salih Dincer
Sep 13, 2022
Erdem
September 10, 2022

import std;

struct A
{
    double val;
    bool isBig;
}


void main() {
    alias DListOfA = DList!A;
    DListOfA[string] temp;
    A a = {2.0, true};

    DListOfA returnVal = temp.require("a", DListOfA());--> I wish I could use ref DListOfA here

    returnVal.insert(a);
    writeln(temp);
}

On the reference page there is an example with class types only (https://dlang.org/spec/hash-map.html) but my type in associative array is a DList which is a struct type and that causes I am having a copy of my DList after require an operation. And of course my insert is happening to the copy and is not affecting to the associative array. ,

I wish I could use ref DListOfA returnVal = .... but we can't in D.

Can you please suggest alternatives?

September 10, 2022
On 9/10/22 09:33, Erdem Demir wrote:

>          DListOfA returnVal = temp.require("a", DListOfA());--> I wish I
> could use ref DListOfA here

But keeping a reference to a temporary would not work because the life of that temporary ends by the end of that expression (practically, at the semicolon).

An option is to allocate the object dynamically with new (and store DListOfA* in the associative array). Then the GC would keep it alive as long its pointer was in the associative arrray.

But a better option is to just forget about it because D already takes care of rvalues by blitting (bit-level copying) them by default. Everything just works... :) It is not expensive either. For example, your struct is very cheap to copy.

But I am probably missing the reason why you want a ref there. Perhaps there are even better options.

Ali

September 10, 2022

On 9/10/22 12:33 PM, Erdem Demir wrote:

>

Can you please suggest alternatives?

Use a pointer.

DListOfA *returnVal = &temp.require(...);
returnVal.insert(a);

-Steve

September 11, 2022

On Saturday, 10 September 2022 at 16:33:03 UTC, Erdem Demir wrote:

>

I wish I could use ref DListOfA returnVal = .... but we can't in D.

Can you please suggest alternatives?

I think you should try advanced update. Your flexibility and what you can do are limited by your dreams. A couple delicious code:

import object, std.container;

struct A
{
    double val;
    bool isBig;
}

void main()
{
    alias  DListOfA = DList!A;
    DListOfA returnVal;
    //DListOfA[string] temp;/*
    DListOfA[string] temp = [
        "a": DListOfA( A(0) )
    ];//*/
    
    auto a = A(6, true); // replacement element
    temp.update("a", {
        return DListOfA( A(0) ); // not updated: unsucceeded but initialized
    }, (ref DListOfA v) {
        v = DListOfA( a ); // existing v has been replaced
        returnVal = v;
        assert(returnVal.front == a);
    });
    assert(is(typeof(temp["a"]) == DList!A));
}

SDB@79

September 13, 2022
On Saturday, 10 September 2022 at 18:38:40 UTC, Ali Çehreli wrote:
> On 9/10/22 09:33, Erdem Demir wrote:
>
> >          DListOfA returnVal = temp.require("a",
> DListOfA());--> I wish I
> > could use ref DListOfA here
>
> But keeping a reference to a temporary would not work because the life of that temporary ends by the end of that expression (practically, at the semicolon).
>
> An option is to allocate the object dynamically with new (and store DListOfA* in the associative array). Then the GC would keep it alive as long its pointer was in the associative arrray.
>
> But a better option is to just forget about it because D already takes care of rvalues by blitting (bit-level copying) them by default. Everything just works... :) It is not expensive either. For example, your struct is very cheap to copy.
>
> But I am probably missing the reason why you want a ref there. Perhaps there are even better options.
>
> Ali

In the code sample I posted I need returnVal.insert(a); have an effect on DListOfA[string] temp; but if returnVal is a rvalue copy I can't accomplish this right?

I see your point about ref would have been assign to a temp val, thanks for pointing out.


September 13, 2022

On Saturday, 10 September 2022 at 18:39:55 UTC, Steven Schveighoffer wrote:

>

On 9/10/22 12:33 PM, Erdem Demir wrote:

>

Can you please suggest alternatives?

Use a pointer.

DListOfA *returnVal = &temp.require(...);
returnVal.insert(a);

-Steve

Actually that could be answer I am seeking for I will try it.

September 13, 2022

On Sunday, 11 September 2022 at 21:01:27 UTC, Salih Dincer wrote:

>

On Saturday, 10 September 2022 at 16:33:03 UTC, Erdem Demir wrote:

>

I wish I could use ref DListOfA returnVal = .... but we can't in D.

Can you please suggest alternatives?

I think you should try advanced update. Your flexibility and what you can do are limited by your dreams. A couple delicious code:

import object, std.container;

struct A
{
    double val;
    bool isBig;
}

void main()
{
    alias  DListOfA = DList!A;
    DListOfA returnVal;
    //DListOfA[string] temp;/*
    DListOfA[string] temp = [
        "a": DListOfA( A(0) )
    ];//*/
    
    auto a = A(6, true); // replacement element
    temp.update("a", {
        return DListOfA( A(0) ); // not updated: unsucceeded but initialized
    }, (ref DListOfA v) {
        v = DListOfA( a ); // existing v has been replaced
        returnVal = v;
        assert(returnVal.front == a);
    });
    assert(is(typeof(temp["a"]) == DList!A));
}

SDB@79

I really like the possibilities of update as well but the overhead is too much especially in this example. I am sorry to say I found old "in" usage less complicated than that even I think "in" is complicated. I liked the Steven's solution all this code can be done via 2 lines only.