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

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


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



<|"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?


1 Answer 1


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 = <||>;
  {"a" :> AssociateTo[ass, "aa" -> "a"], "b" :> AssociateTo[ass, "bb" -> "b"]}

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.