Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 23, 2017 Immutable | ||||
---|---|---|---|---|
| ||||
I am building a system where one thread generates commands and sends them to another thread. The commands will never change once they are created. I have marked the values immutable, but I've been struggling to understand the requirements for sharing a variable across threads. cannot implicitly convert expression (new AppendChatCommand(type, text)) of type data.messages.AppendChatCommand to immutable(AppendChatCommand) I'm trying to do this: immutable(AppendChatCommand) command = new AppendChatCommand(type, text); send(childTid, command); //Compiler does not like command if it's not immutable or shared (and I won't be modifying it or using it in this thread) The commands are generated and send as messages to the child. I will never even use them again in the parent thread. What is the easiest way to do this? Do I need to mark the class somehow? I can provide a contrived example if necessary. |
June 23, 2017 Re: Immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to harakim | On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote:
> immutable(AppendChatCommand) command = new AppendChatCommand(type, text);
try `new immutable AppendChatCommand` instead of just `new`.
If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error.
|
June 23, 2017 Re: Immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to harakim | On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote:
> I am building a system where one thread generates commands and sends them to another thread. The commands will never change once they are created. I have marked the values immutable, but I've been struggling to understand the requirements for sharing a variable across threads.
>
> [...]
heh. I've been working on this for an hour or so. Right after I posted, I tried casting it, which worked. Thank you for your time.
|
June 23, 2017 Re: Immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 23 June 2017 at 14:29:40 UTC, Adam D. Ruppe wrote:
> On Friday, 23 June 2017 at 14:25:48 UTC, harakim wrote:
>> immutable(AppendChatCommand) command = new AppendChatCommand(type, text);
>
> try `new immutable AppendChatCommand` instead of just `new`.
>
> If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error.
Thanks for showing me the right way to do it. My casting strategy just lead to a runtime error.
|
June 23, 2017 Re: Immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to harakim | On Friday, 23 June 2017 at 14:29:41 UTC, harakim wrote:
> heh. I've been working on this for an hour or so. Right after I posted, I tried casting it, which worked. Thank you for your time.
cast works, but `new immutable` with a pure ctor should work better. (casts are easy to get wrong so best to avoid them if there's another way)
|
June 23, 2017 Re: Immutable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 06/23/2017 04:29 PM, Adam D. Ruppe wrote:
> try `new immutable AppendChatCommand` instead of just `new`.
>
> If it complains that it cannot call the mutable constructor, go to the class definition and add `pure` to the constructor. Should take care of that error.
With a `pure` constructor, just `new` should work, too.
|
Copyright © 1999-2021 by the D Language Foundation