2

I have a question about the differences in SetDelayed assignment functions in a Module and in a DynamicModule. In all likelihood, the answer is pretty basic as I am rather new to Mathematica. Nevertheless, I have failed to find the answer in the docs nor in this forum, so I thought I would ask.

The basic problem is the following: SetDelayed does not seem to "delay" when used in DynamicModule, whereas it does in Module. Below I provide code for four different functions that show the behavior.

  • func1 pops up two dialog windows, once for the SetDelayed assignment of insidefunc and once when insidefunc is called (DynamicModule)

  • func2 pops up one dialog window, once when insidefunc is called (Module)

  • func3 pops up one dialog window, once when insidefunc is SetDelayed (DynamicModule)

  • func4 pops up no dialog window


Clear[func1,func2,func3,func4];


func1:=DynamicModule[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];
         insidefunc];

func2:=Module[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];
         insidefunc];

func3:=DynamicModule[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];];

func4:=Module[{insidefunc},
         insidefunc:=DialogInput[CancelButton[]];];

Row[{
    Button["func1", func1["This is function 1"], Method->"Queued"],
    Button["func2", func2, Method->"Queued"],
    Button["func3", func3, Method->"Queued"],
    Button["func4", func4, Method->"Queued"]}]

So, any ideas what I am doing wrong here?

Kuba
  • 136,707
  • 13
  • 279
  • 740
Andy Mobley
  • 352
  • 1
  • 10
  • 1
    SetDelayed is interpreted as Set within DynamicModule. there is really no point in using it. You need to use Initialization to get it to work correctly. – Mike Honeychurch Feb 20 '14 at 12:00
  • Additionally there is nothing dynamic about what you are doing. You should use Module and Button – Mike Honeychurch Feb 20 '14 at 12:35
  • The example I provided was to simplify the problem down to the basic idea. I have plenty of dynamic stuff in the real code. Thanks for your help on this. I'll take a look Initialization and the link by Kuba. – Andy Mobley Feb 20 '14 at 13:03
  • @Kuba, thanks, your link hit on the question exactly. Sorry that I didn't find it before posting. – Andy Mobley Feb 20 '14 at 13:09
  • @AndyMobley No need to be sorry. This is a serious issue not stressed enough in docs. Also, your question is well stated, there are many relevant keywords so it will be a good link to the topic I've linked. – Kuba Feb 20 '14 at 13:11
  • So, now that I have thought about it a bit longer, the answer indicates that my problem is a little deeper than I thought. The real issue is related to Mike's comment because my real code indeed has Dynamic content in it. Therefore, I don't think that I can define the function prior to defining the DynamicModule, although that certainly works (I just tried it out by defining insidefunc in a Module wrapped around the four funcs.) In my code, I can also just use Module instead of DynamicModule and the code works. However, all of my Dynamic variables turn red and that seems bad. Is it? – Andy Mobley Feb 20 '14 at 13:35
  • @Kuba, Well that link is certainly clear. Just before seeing it, I found that I could make the red go away by using the Dynamic as a Pure Function, i.e. Dynamic[#]&[var] where var was the variable that I was using originally (the one that was red). This is in essence the same thing. I think I will go with this as my workaround for now, using Module instead of DynamicModule and switching my few red variables in this way. Thanks so much for your quick and very useful help. I am working on posing the root of my question in a different way, because I think I might learn a lot from that. – Andy Mobley Feb 20 '14 at 14:06
  • @AndyMobley I'm looking forward for your next question. Good luck. – Kuba Feb 20 '14 at 14:14

0 Answers0