“没有可用于类型的清单”错误

时间:2016-05-25 18:22:38

标签: scala

我尝试编写一些Scala类

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参数

Unit => { new C[T]() })

但我怎么能这样做?或者我完全错了?

1 个答案:

答案 0 :(得分:10)

你只需要从顶部传递清单,其中类型是已知的:

from string import Template

template = Template('''\
URL GOTO=https://www.url.com/$user1
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27''')

with open('users.txt') as file:
    for line in file:
        print(template.substitute({'user1': line.strip()}))

应该这样做。