can we add such feature? synchronized(null) ==> i.e NO-OP

Paul Backus snarwin at gmail.com
Fri Aug 26 19:47:18 UTC 2022


On Friday, 26 August 2022 at 19:26:12 UTC, mw wrote:
> On Friday, 26 August 2022 at 18:51:06 UTC, Paul Backus wrote:
>> Since `innerBlock` has access to `foo`'s parameters via its 
>> context, you should not need to do any complicated refactoring.
>
> Yes, it's the same solution I end up with.
>
> However, I still think synchronized(null) ==> NOOP is a better 
> semantics than NPE.
>
> Then I don't even need such innerBlock(), and the if-else.

I guess another possibility is something like this:

```d
void foo(...) {
     scope Object fakeLock = new Object; // allocated on stack
     Object lock = condition ? realLock : fakeLock

     synchronized (lock) {
         // code here
     }
}
```

Since `fakeLock` is local to the function, synchronizing on it 
should be effectively a no-op.


More information about the Digitalmars-d mailing list