Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

scala - Underscore in List.filter

Why this is not working:

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

The error says:

<console>:8: error: missing parameter type for expanded function 
((x$1) => List(true, false).filter(x$1).size)
    List(true,false).filter(_).size
                            ^

but the following works (looks just the same for me):

List(true,false).filter(a => a).size

I'm using Scala 2.9.0.1.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Handling of _ is a bit tricky in Scala and as a side note I think error handling should be improved a bit. Back to topic, have a look at this example:

def twice(i: Int) = i * 2
def gt2(j: Int) = j > 2

List(1,2,3) filter gt2

This compiles fine and works as expected. However trying to compose functions results with cryptic error:

List(1,2,3) filter gt2(twice(_))

  error: missing parameter type for expanded function ((x$1) => twice(x$1))
          List(1,2,3) filter gt2(twice(_))
                                     ^ 

What happened? Basically when Scala compiler sees underscore it binds it into the most immediate context, which is twice(_) in this case. This means we are now calling gt2() with a function twice as an argument. What compiler knows is that this function takes an argument and returns the same type. Arguably it should figure out the type of this argument and return type is Int based on twice() signature, however it uses x$1 placeholder temporarily until he figures that out later.

Unfortunately it is unable to do that since gt2() expects an Int while we are providing a function (at least that is what the compiler thinks).

So why does:

List(1,2,3) filter {k => gt2(twice(k))}

work? The compiler does not know the type of k in advance. However it knows that gt2 returns Boolean and expected an Int. It also knows that twice() expects an Int and returns one as well. This way it infers the type of k. On the other hand the compiler knows from the beginning that filter expects Int => Boolean.


That being said back to your case. The underscore alone ((_)) is not consider a "context" so the compiler searches for another most immediate enclosing context. The !_ would have been considered a function on its own, as well as _ == true. But not the underscore alone.

So what is the closest immediate context (I'm sure there is a scientific name for that...) in this case? Well, the whole expression:

(x$1) => List(true, false).filter(x$1).size

The compiler thinks you are trying to create a function that takes some parameter of unknown type and returns something of the type of an expression: List(true, false).filter(x$1).size. Again arguably it should be able to figure out that filter takes Boolean => Boolean and returns Int (.size), but apparently it doesn't.


So what can you do? You have to give the compiler a hint that the underscore should be interpreted in smaller context. You can say:

List(true,false) filter (_ == true)
List(true,false) filter (i => i)
List(true,false) filter identity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...