Problem with receiveOnly and classes
Ghislain
no_spam at no_spam.com
Sun Mar 25 10:33:00 PDT 2012
Hello,
I need to pass objects of a hierarchy between threads and I have some
troubles.
The sample code below displays:
Unknown
B.fun()
I do not understand why an object of type A is fetched as a Variant, while
a object of type B is received correctly.
Instead of
(A a){a.fun();}
I also tried
(immutable(A) a){a.fun();}
which resulted in
core.exception.AssertError@/usr/include/d/dmd/phobos/std/variant.d(286):
immutable(A)
Any idea?
I am using dmd 2.057 with linux.
Thanks
import std.stdio;
import std.concurrency;
class A{
public:
void fun()const{
writeln("A.fun()");
}
};
class B : A{
public:
override void fun()const{
writeln("B.fun()");
}
};
void producer(Tid t){
auto a = new immutable(A);
auto b = new immutable(B);
t.send(a);
t.send(b);
}
void main(){
auto t = spawn(&producer, thisTid);
while(1){
receive(
(A a){a.fun();},
(Variant v){writeln("Unknown");}
);
}
}
More information about the Digitalmars-d-learn
mailing list