I have a FoldSignal
case class as following.
/**
* 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]
And I used it to create a function in 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)
}
...
}
where Signal[T] is a sealed trait:
/**
* 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] {...}
And it comes up an error:
Error:(71, 22) type mismatch;
found : scala.collection.immutable.Nil.type
required: T
FoldSignal(this, Nil, foldFun)
^
Could someone help me, please! Thanks!
You've messed up types with your (_, DateTime)
- your foldFun
should return U
(see f: (U, T) => U
) which is List[T]
but you return U.head._1
which is Any
(as a result of resolving of your existential type _
)
You see what you see because Scala can't infer a type (for Nil
) here (type inference doesn't see connection between f
and start
parameters), try:
FoldSignal(this, List.empty[T], foldFun)
Another approach is to to separate f
and start
into different parameter lists (start
should be first - so T
is gonna be inferred first from it):
def makeFoldSignal[T, U](s: Signal[T], f: (U, T) => U)(start: U)
May be related and explains about type inference a bit (in comparison with Haskell): 1 :: List[Nothing] in foldLeft