2

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!

Veck Hsiao
  • 591
  • 2
  • 8
  • 20
  • The error message of function foldFun is `Expression of type FoldSignal[T, Any] doesn't conform to expected type Signal[T]` – Veck Hsiao Jul 06 '15 at 06:49

1 Answers1

0

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

Community
  • 1
  • 1
dk14
  • 22,206
  • 4
  • 51
  • 88
  • It won't help, but thanks. BTW, should your `def makeFoldSignal...` be a case class instead of function? – Veck Hsiao Jul 06 '15 at 06:47
  • 1) you've probably messed up types with your (_, DateTime) - your `foldFun` should return `U` (`f: (U, T) => U`)which is `List[T]` but you return `U.head._1` 2) I used factory here be cause you can't have case class with two parameter lists (at least second one won't became `val` automatically and won't have `copy` etc.) – dk14 Jul 06 '15 at 07:36
  • You're right, I truly messed up typed. I think I should consider to refactor my FoldSignal function. – Veck Hsiao Jul 07 '15 at 15:52