“没有可用于类型的清单"错误 [英] "No Manifest available for Type" error

查看:55
本文介绍了“没有可用于类型的清单"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一些 Scala 类

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)

但我得到了:

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]() })}}
                                                                                      ^

据我所知,我需要为 lambda 函数设置隐式 Manifest 参数

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] { ...

应该这样做.

这篇关于“没有可用于类型的清单"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆