Underscore in List.filter

前端 未结 3 2064
夕颜
夕颜 2020-12-02 00:15

Why this is not working:

List(true,false).filter(_).size

The error says:

:8: error: missing parameter type f         


        
3条回答
  •  情书的邮戳
    2020-12-02 00:50

    The first error is because Scala doesn't know what to make of _. So try this...

    List(true,false).filter(_:Boolean).size
    

    After that, you get more info:

    :8: error: type mismatch;
    found   : Boolean
    required: (Boolean) => Boolean
     List(true,false).filter(_:Boolean).size
    

    It's just evaluating the _ just as the value and not as function. Per the ScalaDoc

    filter (pred: (A) ⇒ Boolean): GenTraversable[A] 
    

提交回复
热议问题