How to pack types with variables in one message to send it to another thread? [tuple]

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 7 05:45:09 PDT 2014


On 8/09/2014 12:39 a.m., MarisaLovesUsAll wrote:
> Thanks for reply.
>> Strings are immutable so thats ok. A class instance that isn't
>> immutable isn't.
>
> It's not a class instance, it's a class type. Something like
> `cast(Sprite) null` in parameters. It can be replaced by string
> "Sprite", but in this case I can't use receive() as it is. E.g.
>
> send(tid,gameobjectId,"Sprite","reload");
> //must call sprite.reload();
>
> send(tid,gameobjectId,"Animation","reload");
> //must call animation.reload();

Those calls to send are fine.

>> Just have separate receiving functions per the data type.
> But both messages are (int, string, string) so they can't be separate by
> different receiving functions. It will be better if messages was (int,
> Sprite, string) / (int, Animation, string). And it solves my problem. :)
> But I don't know how to achieve this.

In the given send function calls you don't need to. Just use if 
statements to check the string type.

>> In other words Variant is overkill.
>> It basically just wraps a piece of data so that it can be passed
>> around without knowing its type. Which in this case is bad.
>> You would end up having to know the datatype to do anything with it
>> anyway.
> Then I need something like Variant[] to store this data in array.
>
> MyVariant[] args;
> if(args[0] == typeid(int))
> {
>      if(args[1] == "Sprite") {}
>      if(args[1] == "Animation") {}
> }
> etc.

No need.
http://dlang.org/phobos/std_concurrency.html#.receive

receive(
         (int id, string type, string action) {
		if (type == "Sprite") {
			if (action == "reload")
				mySprite.reload();
		} else if (type == "Animation") {
			if (action == "reload")
				myAnimation.reload();
		}
	}
     );



More information about the Digitalmars-d-learn mailing list