Shared Classes
evilrat
evilrat666 at gmail.com
Sat Nov 9 09:12:46 PST 2013
On Saturday, 9 November 2013 at 16:55:18 UTC, S at S.com wrote:
> It seems to me that the way things are currently implemented
> that a class itself has to be specifically made to handle being
> shared. That is to say, I cannot import some general library
> and do (new shared LibraryType()) if the class doesn't support
> all the proper shared methods. In order for the class to
> properly implement the shared methods, it basically needs to be
> defined as such:
>
> shared class Foo
> {
> ....
> }
>
> But now I still need to do shared Foo everywhere I use that
> class. This seems a bit off to me.
>
>
> R/
> Shammah
actually you also need shared methods/memebers, shared works
similar to const, so for example look at this:
------------------------------------------
import std.concurrency;
import std.stdio;
import core.thread;
// notice no shared at class
class A
{
shared void AddX() { x++; }
shared int x;
}
// accepts shared instances only, so in this way you can't
accidentally modify non-shared instances
void worker(shared A inst)
{
inst.AddX();
writeln(inst.x);
}
void main()
{
shared A a = new shared A();
A b = new A();
// worker(b); // <-- fail!
spawn(&worker, a);
thread_joinAll();
}
-----------------------------------------
so it is just storage specifier like const or immutable. and...
ugh, sorry i'm too crappy on teaching people, but i hope you find
this example somewhat helpful and someone else could give you
more info on this.
More information about the Digitalmars-d
mailing list