digitalmars.D.learn - Immutable
- harakim (18/18) Jun 23 2017 I am building a system where one thread generates commands and
- Adam D. Ruppe (5/7) Jun 23 2017 try `new immutable AppendChatCommand` instead of just `new`.
- harakim (3/10) Jun 23 2017 Thanks for showing me the right way to do it. My casting strategy
- ag0aep6g (2/7) Jun 23 2017 With a `pure` constructor, just `new` should work, too.
- harakim (3/9) Jun 23 2017 heh. I've been working on this for an hour or so. Right after I
- Adam D. Ruppe (4/7) Jun 23 2017 cast works, but `new immutable` with a pure ctor should work
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
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
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:Thanks for showing me the right way to do it. My casting strategy just lead to a runtime error.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
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
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
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