Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to use Hold / ReleaseHold in Mathematica?
    primarykey
    data
    text
    <p>Example and background ( note the usage of Hold, ReleaseHold ):</p> <p>The following code represents a static factory method to create a scenegraph object ( from an XML file ). The (output-)field is an instance of CScenegraph ( an OO-System class ).</p> <pre><code> new[imp_]:= Module[{ ret, type = "TG", record ={{0,0,0},"Root TG"} }, ret = MathNew[ "CScenegraph", 2, MathNew["CTransformationgroup",1,{type,record},0,0,0,0,Null]]; ret@setTree[ret]; ret@getRoot[]@setColref[ret]; csp = loadClass["CSphere"]; spheres = Cases[imp, XMLElement["sphere", _, __], Infinity]; codesp = Cases[spheres, XMLElement["sphere", {"point" -&gt; point_, "radius" -&gt; rad_, "hue" -&gt; hue_}, {}] -&gt; Hold[csp@new[ToExpression[point], ToExpression[rad], ToExpression[hue]]]]; ret@addAschild[ret@getRoot[],ReleaseHold[codesp]]; ret ]; </code></pre> <p>My question is about the following:</p> <pre><code>spheres = Cases[imp, XMLElement[\sphere\, _, __], Infinity]; codesp = Cases[spheres, XMLElement[\sphere\, {\point\ -&gt; point_, \radius\ -&gt; rad_, \"hue\" -&gt; hue_}, {}] -&gt; Hold[csp@new[ToExpression[point], ToExpression[rad], ToExpression[hue]]]]; ret@addAschild[ret@getRoot[],ReleaseHold[codesp]]; </code></pre> <p>where </p> <pre><code> addAschild </code></pre> <p>adds ( a list of ) geometries to a ( root ) transformationgroup and has the signature </p> <pre><code> addAsChild[parent MathObject, child MathObject], or addAsChild[parent MathObject, Children List{MathObject, ...}] </code></pre> <p>and the XML element representing a sphere looks as follows:</p> <pre><code> &lt;sphere point='{0., 1., 3.}' radius='1' hue='0.55' /&gt; </code></pre> <p>If I do NOT USE Hold[] , ReleaseHold[] I end up with objectdata like</p> <pre><code> {"GE", {"SP", {CScenegraph`point, CScenegraph`rad}}, {CScenegraph`hue}} </code></pre> <p>while I would have expected</p> <pre><code> {"GE", {"SP", {{4., 3., -4.}, 3.}}, {0.45}} </code></pre> <p>(The above code with Hold[], ReleaseHold[] yields the correct data.)</p> <h3>Questions</h3> <h3>1. Why is Hold necessary in this case? ( In fact, is it? Is there a way to code this without Hold[], ReleaseHold[]? ) ( I got it right by trial and error! Don't really understand why. )</h3> <h3>2. As a learning point: What is the prototypical example / case for the usage of Hold / ReleaseHold?</h3> <h2>EDIT:</h2> <p>Summary of Leonid's answer. Change this code</p> <pre><code> codesp = Cases[spheres, XMLElement["sphere", {"point" -&gt; point_, "radius" -&gt; rad_, "hue" -&gt; hue_}, {}] -&gt; Hold[csp@new[ToExpression[point], ToExpression[rad], ToExpression[hue]]]]; ret@addAschild[ret@getRoot[],ReleaseHold[codesp]]; </code></pre> <p>to:</p> <pre><code> codesp = Cases[spheres, XMLElement["sphere", {"point" -&gt; point_, "radius" -&gt; rad_, "hue" -&gt; hue_}, {}] :&gt; csp@new[ToExpression[point], ToExpression[rad], ToExpression[hue]]]; ret@addAschild[ret@getRoot[],codesp]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload