0
我想運行DriverTest,但Process.count的類型[I]更改爲[Nothing] ... 此代碼有什麼問題?錯誤的類型推斷[Nothing]
錯誤消息是這樣的: 「發現:程序[沒什麼,INT],需要:進程[字符串,INT]」
sealed trait Process[I, O]
case class Halt[I, O]() extends Process[I, O]
case class Await[I, O](program: Option[I] => Process[I, O]) extends Process[I, O]
case class Emit[I, O](output: O, process: Process[I, O] = Halt[I, O]) extends Process[I, O]
object Driver {
@annotation.tailrec
def driver[I, O](process: Process[I, O], is: Stream[I], os: Stream[O] = Stream()): Stream[O] = process match {
case Halt() => os.reverse //State: Halt -> stop driver
case Await(program) => is match {
case h #:: t => driver(program(Some(h)), t, os) //State: Await + Data: remain -> loop driver
case empty => driver(program(None), empty, os) //State: Await + Data: empty -> go to Halt
}
case Emit(o, process) => driver(process, is, o #:: os) //State: Emit -> save data + loop driver
}
}
object Process {
def loop[I, O, S](s1: S)(f: (I, S) => (O, S)): Process[I, O] = Await[I, O] {
case Some(i) => {
val (o, s2) = f(i, s1)
Emit(o, loop(s2)(f))
}
case None => Halt()
}
def count[I]: Process[I, Int] = loop(0)((i: I, s: Int) => (s + 1, s + 1))
}
object DriverTest extends App {
val input = Stream("a","b","c")
val proc = Process.count
val output = Driver.driver(proc, input)
}
它的工作原理,但它是相當整潔添加[字符串]或某些情況下的情況。沒有這種類型的標籤,沒有更通用的解決方案嗎? – Curycu
'val proc = Process.count [Any]'可以擺脫整齊的東西...但是... – Curycu
有沒有信息count方法在哪裏採取'type'從。這就是爲什麼你沒有任何東西。如果你想防止這種情況,你必須幫助scala推理類型。你可以做前例。將'input'作爲'count'方法的參數。 – Rumoku