I try to write some Scala classes
abstract class A { var a : Int = _}
class B[T] extends A { var b : T = _ }
class C[T] extends A { var c : T = _ }
class Abc[T : Manifest] {
    var array : Array[T] = _
    def this(capacity : Int, f : Unit => T) = {
        this()
        array = new Array[T](capacity)
        for(i <- 0 until capacity)
            array(i) = f()
    }
}
class Xyz[T] { 
    var m : Abc[C[T]] = _; 
    def this(capacity : Int) = { 
    this(); 
    m = new Abc[C[T]](capacity, Unit => { new C[T]() })
    }
}
var xyz = new Xyz[Int](10)
But I got:
error: No Manifest available for C[T].
       class Xyz[T] { var m : Abc[C[T]] = _; def this(capacity : Int) = { this(); m = new Abc[C[T]](capacity, Unit => { new C[T]() })}}
                                                                                      ^
As far as I understand I need to set up implicit Manifest argument for the lambda function
Unit => { new C[T]() })
But how can I do that? Or I am completly wrong?
You just need to carry the manifest all the way from the top, where the type is known:
class Xyz[T : Manifest] { ...
should do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With