title: Using type parameters and mixins in Scala
body: <p><strong>EDIT 2:</strong></p>
  
  <p>I managed to achieve the type safety I wanted in my exercise with RomanNumerals using a combination of mixins and type parameters with the code below. In essence what it does is after importing everything in <code>RomanNumerals</code> I am able to write <code>L X V I</code>, but not <code>L L</code> or <code>X L X</code> cause then I get a type mismatch compiler error  (since those would be illegal combinations of roman numerals). Now I'm wondering if this way of using traits, mixins and type parameters is considered ok or if I'm abusing the language so to speak :) Is there a better way of achieving the same kind of type safety with some simpler/cleaner code?</p>
  
  <pre><code>object RomanNumerals {
    trait Numeral {
      def num:Int
    }
    trait NumeralI[N&lt;:Numeral] extends Numeral
    trait NumeralV[N&lt;:Numeral] extends NumeralI[N] {
      def I = new RomanNumeral[Numeral](1 + this.num) with Numeral
      def II = new RomanNumeral[Numeral](2 + this.num) with Numeral
      def III = new RomanNumeral[Numeral](3 + this.num) with Numeral
    }
  
    trait NumeralX[N&lt;:Numeral] extends NumeralV[N] {
      def IV = new RomanNumeral[Numeral](4 
                + this.num) with Numeral
      def V = new RomanNumeral[NumeralI[Numeral]](5 
               + this.num) with NumeralV[NumeralI[Numeral]]
      def IX = new RomanNumeral[Numeral](9
                + this.num) with Numeral
    }
  
    trait NumeralL[N&lt;:Numeral] extends NumeralX[N] {
      def X = new RomanNumeral[NumeralV[Numeral]](10
               + this.num) with NumeralX[NumeralV[Numeral]]
      def XX = new RomanNumeral[NumeralV[Numeral]](20 
                + this.num) with NumeralX[NumeralV[Numeral]]
      def XXX = new RomanNumeral[NumeralV[Numeral]](30 
                 + this.num) with NumeralX[NumeralV[Numeral]]
    }
  
    class RomanNumeral[T &lt;: Numeral](val num:Int) {
      override def toString = num toString
      def apply[N &gt;: T &lt;: Numeral](rn:NumeralI[N]) = 
        new RomanNumeral[Numeral](rn.num + num) with Numeral
  
      def apply[N &gt;: T &lt;: Numeral](rn:NumeralV[N]) = 
        new RomanNumeral[NumeralI[Numeral]](rn.num
         + num) with NumeralV[NumeralI[Numeral]]
  
      def apply[N &gt;: T &lt;: Numeral](rn:NumeralX[N]) = 
        new RomanNumeral[NumeralV[Numeral]](rn.num 
         + num) with NumeralX[NumeralV[Numeral]]
  
      def apply[N &gt;: T &lt;: Numeral](rn:NumeralL[N]) = 
        new RomanNumeral[NumeralX[Numeral]](rn.num 
         + num) with NumeralL[NumeralX[Numeral]]
  
    }
  
    val I = new RomanNumeral[NumeralI[Numeral]](1) with NumeralI[Numeral]
    val II = new RomanNumeral[NumeralI[Numeral]](2) with NumeralI[Numeral]
    val III = new RomanNumeral[NumeralI[Numeral]](3) with NumeralI[Numeral]
    val IV = new RomanNumeral[NumeralI[Numeral]](4) with NumeralI[Numeral]
    val V = new RomanNumeral[NumeralI[Numeral]](5) with NumeralV[NumeralV[Numeral]]
    val IX = new RomanNumeral[NumeralI[Numeral]](9) with NumeralI[Numeral]
    val X = new RomanNumeral[NumeralV[Numeral]](10) with NumeralX[NumeralX[Numeral]]
    val XX = new RomanNumeral[NumeralV[Numeral]](20) with NumeralX[NumeralX[Numeral]]
    val XXX = new RomanNumeral[NumeralV[Numeral]](30) with NumeralX[NumeralX[Numeral]]
    val XL = new RomanNumeral[NumeralV[Numeral]](40) with NumeralX[NumeralX[Numeral]]
    val L = new RomanNumeral[NumeralX[Numeral]](50) with NumeralL[NumeralL[Numeral]] 
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Further question based of Victors answere. Ok, but what if I add upper and lower bounds to the type parameter so that B is a trait? E.g.</p>
  
  <pre><code>trait Bar
  class Foo[T&lt;:Bar](n:Int) {
    def apply[B &gt;: T &lt;: Bar](f:Foo[B]) = {
      new Foo[B](n + f.n) with B
    }
  }
  </code></pre>
  
  <p>Or maybe <code>B</code> can still be a class in this case? What if I know that the f argument too apply is of type <code>Foo[B] with B</code>? Is there a way to use that to mixin <code>B</code> with the return type?</p>
  
  <p><strong>ORIGINAL QUESTION BELOW</strong></p>
  
  <p>I want to mix in a trait I get as a type parameter when I create an object in Scala:</p>
  
  <pre><code>class Foo(val num:Int) { 
    def withM[B](foo:Foo) = new Foo(foo.num) with B
  }
  </code></pre>
  
  <p>This results in a compile error:</p>
  
  <pre><code>error: class type required but B found
  def withM[B](foo:Foo) = new Foo(foo.num) with B
                                                ^
  </code></pre>
  
  <p>I have also tried: </p>
  
  <pre><code>class Foo(val num:Int) { 
    def withM[B](foo:Foo) = new Foo(foo.num) with classOf[B]
  }
  </code></pre>
  
  <p>But that does not work:</p>
  
  <pre><code>error: not found: type classOf
  def withM[B](foo:Foo) = new Foo(foo.num) with classOf[B]
                                                ^
  </code></pre>
  
  <p>Is there someway to get around this? So that the return type of withM becomes <code>Foo with B</code> where <code>B</code> is the type parameter passed to <code>withM</code></p>