www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Immutable

reply harakim <chrome.user.because gmail.com> writes:
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.
Jun 23 2017
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Jun 23 2017
next sibling parent harakim <chrome.user.because gmail.com> writes:
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.
Jun 23 2017
prev sibling parent ag0aep6g <anonymous example.com> writes:
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.
Jun 23 2017
prev sibling parent reply harakim <chrome.user.because gmail.com> writes:
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.
Jun 23 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
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)
Jun 23 2017