4
$\begingroup$

Assigning Nothing to a variable inside Module works as expected:

Module[{x, y},
 x = 1;
 y = Nothing;
 {x, y}]
{1}

However, using DynamicModule instead triggers an error:

DynamicModule[{x, y},
 x = 1;
 y = Nothing;
 {x, y}]
Transpose::nmtx: The first two levels of {{Hold[x],Hold[y]},{Hold[1]}} cannot be transposed.
Manipulate`Dump`eDynamicModule[
Transpose[
Manipulate`Dump`heldsetting[{{Hold[x], Hold[y]}, {Hold[1]}}]], {1}, 
DynamicModuleValues :> {}]

I came across this error combining DynamicModule with Manipulate, getting a different error message:

DynamicModule[{x, y},
 Manipulate[
  x = 1;
  y = Nothing;
  {x, y, z},
  {z, 1, 10}]]
MapThread::mptc: Incompatible dimensions of objects at positions {2, 2} and {2, 3} of
MapThread[FrontEnd`Private`newvalue,
{{1,2},
  {Hold[FE`x$$367],Hold[FE`y$$367]},
  {Hold[1]},
  {Hold[1]}}
]; dimensions are 2 and 1.

Screenshot of Mathematica output

Now of course one can easily work around these errors, e.g. writing

DynamicModule[{x, y},
 x = 1;
 If[IWantToReturnNothing[], {x}, {x, y}],
 Initialization :> (IWantToReturnNothing[] := True)]
{1}

where the function IWantToReturnNothing[] evaluates whether I want to assign Nothing to the variable $y$. So I am not asking for a solution/workaround, I am just curious as to why this happens to better understand Mathematica. I suspect this behaviour has to do with the point of time Hold is released from Hold[Nothing], but I don't know why Mathematica would release Hold before initialization?

Thank you in advance!

$\endgroup$
2
  • 4
    $\begingroup$ Sounds like a bug to me. $\endgroup$ – Sjoerd Smit Feb 5 at 12:51
  • $\begingroup$ I'll report this with the appropriate development team. Something is certainly going wrong here. $\endgroup$ – ktm Feb 5 at 17:06

Your Answer

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

Browse other questions tagged or ask your own question.