2
$\begingroup$

Consider the 3 following manually entered evaluations where a Set statement is brought to the Front-End by Dynamic:

Unexpected

Obviously the Set statement in Dynamic is evaluated only once, at the beginning. Despite remaining on the screen at all times and despite manual future alteration of t, t=0 does not seem to be evaluated again. According to documentation I would expect the Set statement to be evaluated each time t changes.

Why is this so? I failed to see the explanation in the neat answers Using Refresh[..] with TrackedSymbols! and advanced Dynamic documentation.

This is further surprising, considering that adding t; in the Dynamic expression, recovers systematic evaluation of t=0 when t is altered:

Expected

Later increment: following the suggestion by Kuba in the comments about using t[], here is a third case where t[0] apparently does not depend on t (like the first case) but actually updates this time.

enter image description here

Thank you for you help and keep on the good work.

$\endgroup$
  • $\begingroup$ The statement t = 0 does not depend on the value of t. The statement t; t = 0 does, since t is evaluated. $\endgroup$ – Michael E2 Dec 8 '18 at 20:59
  • $\begingroup$ Ok, it is not sufficient that t formally (lexically) appears in the expression to be updated by Dynamic. I better understand the logics now, somehow clearer than the multipage documentation, thank you. $\endgroup$ – SteppingStone Dec 8 '18 at 21:24
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – Kuba Dec 8 '18 at 22:14
  • 1
    $\begingroup$ +1 -- If @Kuba & I can have an argument about the correct answer to a question, without the OP having to clarify the question at all, then the question deserves an upvote from me. :) $\endgroup$ – Michael E2 Dec 12 '18 at 20:00
1
$\begingroup$

I think the following two criteria explain why Dynamic[t = 0] does not update and Dynamic[t; t = 0] updates.

Suppose we have Dynamic[code] where code is some relatively simple code depending only on a symbol t. Then code will be update (executed) dynamic when

  1. t is evaluated when code is evaluated and
  2. t has changed its value.

There are several abstruse examples in the chat conversation, but I believe they are all explained by these criteria.

Dynamic[t = 0]: Set does not evaluated the LHS t, although it might change its value. Since criterion 1 is not satisfied, there is no update even if condition 2 is satisfied (when t = 1 is executed and changes the value of t).

Dynamic[t; t = 0]: Now t is evaluated before Set. Since criterion 1 is satisfied, there is an update when condition 2 is satisfied (when t = 1 is executed and changes the value of t).

Dynamic[t[] = 0]: Set does not evaluated the LHS t[] but the head t is evaluated. Since criterion 1 is satisfied, there is an update when condition 2 is satisfied (when t[] = 1 is executed and changes the value of t).

Dynamic[t[[1]] = 0]: Assuming t has been appropriately initialized (e.g. t = {1}), Set does not evaluated the LHS t[[1]] nor does it evaluate t. The FullForm of the LHS is Part[t, 1], and presumably the head Part is evaluated. In order to alter t, Set cannot evaluate t in this case. Since criterion 1 is not satisfied, there is no update even if condition 2 is satisfied (when t[[1]] = 1 is executed and changes the value of t).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.