title: Implicit conversions in the context of a (case) class constructor
body: <p>I would like to have the automatic companion class <code>apply</code> constructors of a case class to perform implicit conversions for me, but cannot figure out how to do so. I've searched all over and the closest answer I could find was for <a href="https://stackoverflow.com/questions/7297291/can-a-method-argument-serve-as-an-implicit-parameter-to-an-implicit-conversion">this</a> question (I'll explain why it isn't what I'm looking for below).</p>
  
  <p>I have a case class that looks something like this:</p>
  
  <pre><code>case class Container(a: Long, b: Long, c: Long)
  </code></pre>
  
  <p>I'm using the container to count instances where certain conditions apply, so I'd like to be able to have the constructor automatically convert boolean parameters to longs (<code>if (boolean) 1L else 0L</code>).</p>
  
  <p>The real case class, of course, has many parameters, so it would be tedious and terribly repetitive to make my own companion object and overload <code>apply</code> to accept <code>Boolean</code> parameters. Additionally, something like the code below isn't ideal (if it were implemented correctly somehow) as it only accepts boolean arguments:</p>
  
  <pre><code>object Container {
    def apply(args: Boolean*) = {
      // doesn't REALLY work since number of arguments not enforced
      Container(args map { if (_) 1L else 0L } toArray: _*)
    }
  }
  val c1 = Container(1, 0, 1) // works
  val c2 = Container(true, false, true) // might be workable if done correctly
  val c3 = Container(true, 0, 1) // won't work
  </code></pre>
  
  <p>I tried adding an implicit conversion in the companion object (below), hoping that it would automatically be used in <code>Container.apply</code>, but it appears that this does not actually put the implicit conversion into the namespace of the code that calls apply.</p>
  
  <pre><code>object Container {
    implicit def booleanToLong(x: Boolean): Long = if (x) 1L else 0L
  }
  </code></pre>
  
  <p>I'm able to get things working using this hackish workaround:</p>
  
  <pre><code>{
    import Container.booleanToLong
    // all of these now work
    val c1 = Container(1, 0, 1)
    val c2 = Container(true, false, true)
    val c3 = Container(true, 0, 1) // works!!!
  }
  </code></pre>
  
  <p>The biggest problem is that I have to import <code>booleanToLong</code> into the code that wants to create a <code>Container</code> and thus must put it in its own block for safety (<code>booleanToLong</code> is generally undesirable).</p>
  
  <p>Finally, the solution of using an implicit parameter that itself includes an implicit conversion doesn't work because it would require an explicit overriding of <code>apply</code>, defeating the goal of not repeating a long parameter list and marshaling types.</p>
  
  <p>Is there a way to do this such that I get implicit conversions for free every time I make a <code>Container</code>, but <em>not</em> otherwise? Or is this impossible due to some sort of technical constraint?</p>