D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6823 - Shared Delegates Should Implicitly Cast to Nonshared
Summary: Shared Delegates Should Implicitly Cast to Nonshared
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-17 23:58 UTC by Andrew Wiley
Modified: 2021-01-24 06:20 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrew Wiley 2011-10-17 23:58:06 UTC
Code Example:
synchronized class Thing {
        void doSomeWork(void delegate() work) {
                work();
        }
        void work() {}
}

void main() {
        auto th = new Thing();
        th.doSomeWork(&th.work);
}

This example won't compile because the type of `&th.work` is `void delegate() shared`, which will not implicitly convert to `void delegate()`.
This type is probably correct - it's safe to share that delegate between threads, but since it's also safe to NOT share that delegate between threads, the delegate should implicitly cast to non-shared.

This is a large problem for code using shared data because `void delegate() shared` does not parse as a type.

The current workaround is to insert a cast to `void delegate()` at the call site.
Comment 1 mhh 2021-01-24 06:20:05 UTC
I'm not sure on the type conversion aspect but `void delegate() shared` has worked for years (at least < 2.060LD)