how to assign to shared obj.systime?

Steven Schveighoffer schveiguy at gmail.com
Fri Jul 10 17:35:56 UTC 2020


On 7/10/20 1:18 PM, mw wrote:
> On Friday, 10 July 2020 at 08:48:38 UTC, Kagamin wrote:
>> On Friday, 10 July 2020 at 05:12:06 UTC, mw wrote:
>>> looks like we still have to cast:
>>> as of 2020, sigh.
>>
>> Why not?
> 
> Because cast is ugly.
> 
> I've also tried this:
> ```
> class A {
>          SysTime time;
>          synchronized setTime(ref SysTime t) {
>                  time = t;
>          }
> }
> 
> void main() {
>          shared A a = new A();
>          SysTime time;
>          a.setTime(time);
> }
> ```
> 
> Same Error: template std.datetime.systime.SysTime.opAssign cannot deduce 
> function from argument types !()(SysTime) shared, candidates are:
> /usr/include/dmd/phobos/std/datetime/systime.d(659,17): opAssign()(auto 
> ref const(SysTime) rhs)
> 
> However, we have a lock on the owning shared object, still we need cast 
> to make it compile:
> ```
>        cast()time = t;
> ```
> 

Mark your setTime as shared, then cast away shared (as you don't need 
atomics once it's locked), and assign:

synchronized setTime(ref SysTime t) shared {
     (cast()this).time = t;
}

-Steve


More information about the Digitalmars-d-learn mailing list