我有一个FoldSignal
案例类如下。
/**
* Represents a signal that is manipulated to using the functional "fold" operation
* @param s Underlying signal
* @param start seed for the fold
* @param f fold function
*/
case class FoldSignal[T, U](s: Signal[T], start: U, f: (U, T) => U) extends Signal[U]
我用它在Signal [T]中创建了一个函数:
sealed trait Signal[T]{
...
/**
* Return a delta of signals in windows
*/
def delta(implicit ev: T =:= (_, DateTime) ): Signal[T] = {
def foldFun(queue: List[T], t: T) = {
println(queue(0))
queue(0)._1
}
FoldSignal(this, Nil, foldFun)
}
...
}
其中Signal [T]是密封的特征:
/**
* Signal is a AST object represents a value that is continuously emitted. It does
* not specify anything about the source of the signal.
*/
sealed trait Signal[T] {...}
它出现了错误:
Error:(71, 22) type mismatch;
found : scala.collection.immutable.Nil.type
required: T
FoldSignal(this, Nil, foldFun)
^
请有人帮助我!谢谢!
答案 0 :(得分:0)
您已使用(_, DateTime)
搞乱了类型 - 您的foldFun
应该返回U
(请参阅f: (U, T) => U
)List[T]
但您返回U.head._1
Any
(由于解析了您的存在类型_
)
您看到了所看到的内容,因为Scala无法在此处推断出类型(Nil
)(类型推断不会看到f
和start
参数之间的关联),试试:
FoldSignal(this, List.empty[T], foldFun)
另一种方法是将f
和start
分成不同的参数列表(start
应该是第一个 - 所以T
将首先从中推断出来:< / p>
def makeFoldSignal[T, U](s: Signal[T], f: (U, T) => U)(start: U)
可能是相关的并且稍微解释了类型推断(与Haskell相比):1 :: List[Nothing] in foldLeft