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

前端 未结 3 1374
耶瑟儿~
耶瑟儿~ 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 18:55

    1) The second one does not produce a warning probably because you are constructing the list (or the pattern) by prepending elements to the Nil object, which extends List parameterising it with Nothing. And since everything is Nothing, there is nothing to be worried about ;) But I'm not sure, really guessing here.

    2) Why don't you just use:

    def foo[A](ls: Iterable[A]) =
        for (list <- ls sliding 2) yield ()
    

提交回复
热议问题