title: Implicit in Scala 'for' enumerator?
body: <p>How can you make a variable introduced in a <code>for</code> statement (an Enumerator, as it's called in the spec) implicit?</p>
  
  <p>Here's an example of what I'd <em>like</em> to write:</p>
  
  <pre><code>for (implicit genNum: GenNum &lt;- NumGens(200)) {
    val explicitArg = // . . .
    funcWithImplicitGenNum(explicitArg)  // &lt;-- gen passed implicitly here
  }
  </code></pre>
  
  <p>where <code>funcWithImplicitGenNum</code> is declared like this:</p>
  
  <pre><code>def funcWithImplicitGenNum(explicitArg: Whatever)(implicit gen: GenNum) = ???
  </code></pre>
  
  <p>If I understand the spec correctly, it doesn't allow a <code>for</code>'s Enumerator to be implicit, so I have to do this:</p>
  
  <pre><code>for (g &lt;- NumGens(200)) {
    implicit val genNum: GenNum = g
    val explicitArg = // . . .
    funcWithImplicitGenNum(explicitArg)  // &lt;-- gen passed implicitly here
  }
  </code></pre>
  
  <p>This workaround is not the end of the world, but it bothers me. In my actual program, I pass several variables implicitly all over the place, which provide the overall context in which something is happening. Scala's implicits have been wonderful for this: they greatly reduce the amount of clutter in the code, yet functions deep in the call stack have access to that contextual information. The only exception is <code>for</code> loops: I have to do this clumsy trick of defining a <code>val</code> that equals the enumeration variable.</p>
  
  <p>Is there a way to pass <code>genNum</code> implicitly? Or, if not, do you know of a reason why I should be glad that the <code>for</code> statement doesn't allow its Enumerator to be made implicit? (Often, if you know the rationale for a language feature, you see better how to "go with the grain" of the language.)</p>
  
  <hr>
  
  <p>Here's a little more background, if it helps. I'm writing a genetic algorithm that uses a variety of mutation operators. A mutation operator shouldn't know which generation it's being called for. But I'm also collecting data about how the mutations perform in different generations. The data-collection code <em>does</em> need to know which generation it is, plus a few other things. So, I pass the variables that pertain to mutation explicitly, and the variables needed for data collection implicitly.</p>