Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn

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:


```d
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.





Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn
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.

```d
DListOfA *returnVal = (...);
returnVal.insert(a);
```

-Steve


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


Re: Using .require for struct types

2022-09-13 Thread Erdem via Digitalmars-d-learn

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.





Re: Using .require for struct types

2022-09-11 Thread Salih Dincer via Digitalmars-d-learn

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:


```d
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


Re: Using .require for struct types

2022-09-10 Thread Ali Çehreli via Digitalmars-d-learn

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



Re: Using .require for struct types

2022-09-10 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Can you please suggest alternatives?



Use a pointer.

```d
DListOfA *returnVal = (...);
returnVal.insert(a);
```

-Steve