title: promise already under evaluation: recursive default argument reference or earlier problems?
body: <p>Here is my R code. The functions are defined as:</p>
  
  <pre><code>f &lt;- function(x, T) {
    10 * sin(0.3 * x) * sin(1.3 * x ^ 2) + 0.001 * x ^ 3 + 0.2 * x + 80
  }
  
  g &lt;- function(x, T, f=f) {
    exp(-f(x) / T)
  }
  
  test &lt;- function(g=g, T=1) { 
    g(1, T)
  }
  </code></pre>
  
  <p>The running error is:</p>
  
  <blockquote>
    <p>> test()<br>
    Error in test() :<br>
     promise already under evaluation: recursive default argument reference or earlier problems?</p>
  </blockquote>
  
  <p>If I substitute the definition of <code>f</code> in that of <code>g</code>, then the error goes away.</p>
  
  <p>I was wondering what the error was? How to correct it if don't substitute the definition of <code>f</code> in that of <code>g</code>? Thanks!</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>Thanks! Two questions:</p>
  
  <p>(1) if function <code>test</code> further takes an argument for <code>f</code>, will you add something like <code>test &lt;- function(g.=g, T=1, f..=f){ g.(1,T, f.=f..) }</code> ? In cases with more recursions, is it a good and safe practice adding more <em>.</em>? </p>
  
  <p>(2) if <code>f</code> is a non-function argument, for example <code>g &lt;- function(x, T, f=f){ exp(-f*x/T) }</code> and <code>test &lt;- function(g.=g, T=1, f=f){ g.(1,T, f=f.) }</code>, will using the same name for both formal and actual non-functional arguments a good and safe practice or it may cause some potential trouble? </p>