Why does Scala warn about type erasure in the first case but not the second?

前端 未结 3 1297
耶瑟儿~
耶瑟儿~ 2021-02-20 18:39

I have two functions (not these have been edited since the original -- some of the answers below are responding to the original ones which returned a sequence of ()):

         


        
3条回答
  •  無奈伤痛
    2021-02-20 19:13

    I'm not sure what is happening here, but the static type of Iterable[A].sliding is Iterator[Iterable[A]], not Iterator[List[A]] which would be the static type of List[A].sliding.

    You can try receiving Seq instead of Iterable, and that work too. EDIT Contrary to what I previously claimed, both Iterable and Seq are co-variant, so I don't know what's different. END EDIT The definition of sliding is pretty weird too:

    def sliding [B >: A] (size: Int): Iterator[Iterable[A]]
    

    See how it requires a B, superclass of A, that never gets used? Contrast that with an Iterator.sliding, for which there's no problem:

    def sliding [B >: A] (size: Int, step: Int = 1): GroupedIterator[B]
    

    Anyway, on to the second case:

    for (a::b::Nil <- ls sliding 2) yield a
    

    Here you are decomposing the list twice, and for each decomposition the type of head is checked against A. Since the type of head is not erased, you don't have a problem. This is also mostly a guess.

    Finally, if you turn ls into a List, you won't have a problem. Short of that, I don't think there's anything you can do. Otherwise, you can also write this:

    def foo1[A](ls: Iterable[A]) : Iterator[A] =
        for (Seq(a, b) <- ls.iterator sliding 2) yield a
    

提交回复
热议问题