So how do you create an associative array with an immutable value, anyways?

Steven Schveighoffer schveiguy at gmail.com
Fri Oct 7 18:00:56 UTC 2022


On 10/7/22 12:11 PM, FeepingCreature wrote:
> On Friday, 7 October 2022 at 16:03:49 UTC, Steven Schveighoffer wrote:
>> On 10/7/22 11:27 AM, FeepingCreature wrote:
>>> It's hard to see how you'd make `require` work but not `assocArray`, 
>>> since `require` is not exactly a built-in feature either.
>>
>> You can either cast (which is reasonable, considering that the 
>> assignment only happens when it didn't already exist) or make a new hook.
>>
> 
> `cast` is not sufficient because `immutable struct S` cannot by any 
> means be made mutable.

`copyEmplace` appears to work:

```d
import core.lifetime;
immutable struct S
{
     int x;
}
void main()
{
     S s = S(5);
     ubyte[S.sizeof] buf;
     copyEmplace(s, *cast(S*)buf.ptr);
}
```

-Steve


More information about the Digitalmars-d mailing list