title: Mixing Scala and Java: How to get generically typed constructor parameter right?
body: <p>I have some legacy Java code that defines a generic <code>payload</code> variable <strong>somewhere outside of my control</strong> (i.e. I can not change its type):</p>
  
  <pre><code>// Java code
  Wrapper&lt;? extends SomeBaseType&gt; payload = ...
  </code></pre>
  
  <p>I <strong>receive</strong> such a <code>payload</code> value as a method parameter in my code and want to pass it on to a Scala <code>case class</code>  (to use as message with an actor system), but do not get the definitions right such that I do not get at least a compiler warning. </p>
  
  <pre><code>// still Java code
  ScalaMessage msg = new ScalaMessage(payload);
  </code></pre>
  
  <p>This gives a compiler warning "Type safety: contructor... belongs to raw type..."</p>
  
  <p>The Scala <code>case class</code> is defined as:</p>
  
  <pre><code>// Scala code
  case class ScalaMessage[T &lt;: SomeBaseType](payload: Wrapper[T]) 
  </code></pre>
  
  <p>How can I define the case class such that the code compiles cleanly? (sadly, changing the code of the Java <code>Wrapper</code> class or the type of the <code>payload</code> parameter is not an option)</p>
  
  <p><strong>Updated</strong> to clarify the origin of the payload <strong>parameter</strong></p>
  
  <p><strong>Added</strong> For comparison, in Java I can define a parameter just in the same way as the <code>payload</code> variable is defined:</p>
  
  <pre><code>// Java code
  void doSomethingWith(Wrapper&lt;? extends SomeBaseType&gt; payload) {}
  </code></pre>
  
  <p>and call it accordingly</p>
  
  <pre><code>// Java code
  doSomethingWith(payload)
  </code></pre>
  
  <p>But I can't instantiate e.g. a Wrapper object directly without getting a "raw type" warning. Here, I need to use a <code>static</code> helper method:</p>
  
  <pre><code>static &lt;T&gt; Wrapper&lt;T&gt; of(T value) {
     return new Wrapper&lt;T&gt;(value);
  }
  </code></pre>
  
  <p>and use this static helper to instantiate a <code>Wrapper</code> object:</p>
  
  <pre><code>// Java code
  MyDerivedType value = ... // constructed elsewhere, actual type is not known!
  Wrapper&lt;? extends SomeBaseType&gt; payload = Wrapper.of(value);
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>I can add a similar helper method to a Scala companion object:</p>
  
  <pre><code>// Scala code
  object ScalaMessageHelper {
      def apply[T &lt;: SomeBaseType](payload: Wrapper[T]) = 
          new ScalaMessage(payload)
  }
  object ScalaMessageHelper2 {
      def apply[T &lt;: SomeBaseType](payload: Wrapper[T]) = 
          ScalaMessage(payload) // uses implicit apply() method of case class
  }
  </code></pre>
  
  <p>and use this from Java to instantiate the <code>ScalaMessage</code> class w/o problems:</p>
  
  <pre><code>// Java code
  ScalaMessage msg = ScalaMessageHelper.apply(payload);
  </code></pre>
  
  <p>Unless someone comes up with a more elegant solution, I will extract this as an answer...</p>
  
  <p>Thank you!</p>