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

前端 未结 3 1375
耶瑟儿~
耶瑟儿~ 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:15

    I'm not really familiar with Scala, but I have used Java and Haskell, so I'm going to go out on a limb here and guess that your second example isn't doing quite the same thing. It looks like both of them are using a pattern match to deconstruct the first two elements of a list, but the second one is using the cons operator as opposed to the List constructor. My guess would be that this is somehow related to the interop with Java and a subtle difference between the way the List constructor versus the cons operator actually function.

    On the other hand, maybe it's just an edge case that got overlooked by the compiler and they both should generate a warning.

提交回复
热议问题