1
$\begingroup$

While trying to use the ChoiceDialog function to populate an association, I have run into an interesting issue...

ass=<||>;
ChoiceDialog["which", {"a" -> AssociateTo[ass, "aa" -> "a"], 
  "b" -> AssociateTo[ass, "bb" -> "b"], 
  AssociateTo[ass, "c" -> "cc"]}]

After running, and only selecting one option, such as "a" in the choicedialog window, it properly prints out

<|aa->a|>

But then upon re-calling the association ass, which was not previously populated with anything, now all values have been added to it:

ass

yields

<|"aa" -> "a", "bb" -> "b", "c" -> "cc"|>

Why are all values that were placed in the Dialog box being added, and not only the value which I have selected?

$\endgroup$

1 Answer 1

3
$\begingroup$

This happens because you're using Rule (->) here to specify the result action for the buttons. Rule does not hold its arguments, so all the rules are evaluated during the creation of the window.

If the buttons of your ChoiceDialog have side-effects (e.g., they modify a variable), then you should use RuleDelayed (:>) which holds the 2nd argument. Otherwise all side-effects will trigger.

ass = <||>;
ChoiceDialog["which",
  {"a" :> AssociateTo[ass, "aa" -> "a"], "b" :> AssociateTo[ass, "bb" -> "b"]}
]
ass
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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