shared members and castings
Steven Schveighoffer
schveiguy at yahoo.com
Mon Nov 14 04:58:39 PST 2011
On Sat, 12 Nov 2011 15:20:54 -0500, nrgyzer <nrgyzer at gmail.com> wrote:
> Hi guys,
>
> is there any way to use shared members without casting them? Fox example:
>
> class Example {
>
> private shared HashSet!(string) ex;
>
> ...
>
> this() {
> ex = cast(shared) new HashSet!(string)();
> }
>
> void write() {
> foreach (ref c; cast(HashSet!(string)) ex) {
> std.stdio.writeln(c);
> }
> }
> }
>
> Without casting, I always get some errors. My classes contains many
> different
> collections and values, so I've many casts which makes the code at some
> points
> a bit unclear. Is there any way to prevent the casting from/to shared
> objects?
The objects have to implement shared methods directly. From your sample
code, I assume you are using dcollections.
I have not yet thought about how to tackle shared versions of containers.
Certainly, your code is not a good way to do it, since you are not doing
any synchronization.
The issue is, if a container is shared, you may only call shared methods
on it. I have no shared methods in dcollections, because they would be
simple wrappers for synchronizing the methods on the container. I would
like to find an automatic way to do this rather than use boilerplate
everywhere.
I also would like to investigate shared-aware containers that are safer to
use (possibly lock-free) than simply synchronizing all methods.
-Steve
More information about the Digitalmars-d-learn
mailing list