2

Suppose this very simple example:

def complexMethod (first : Int, second : Int) : Boolean = {
  //heavy stuff goes in here
  true
}

val myList : List[Int] = List(1,2,3)

val newList : List[Int] = myList.filter(_ => {complexMethod(_, 2)})

Im getting a message that says "Type mismatch, expected: Int => Boolean, actual: Int => Int => Boolean"

How is that up above not Int => Boolean ??

Basically I want to send the item to a function where it will perform some logic and return a boolean. What is the proper way to do it and respect the "Int => Boolean" ?

Thank you

N.Car
  • 492
  • 1
  • 4
  • 14
  • 2
    The first _underscore_ (`_ =>`) means here comes argument of the function, but I want to ignore it. The second one (`(_, 2)`) means create an anonymous function with one argument and pass it as the first argument to `complexMethod`. In resume it is of type `Int => Int => Boolean`. You may either just write `myList.filter(complexMethod(_, 2))` or `myList.filter(i => {complexMethod(i, 2)})` _(put whatever name you want)_. – Luis Miguel Mejía Suárez Mar 22 '19 at 18:30
  • 2
    Why is it not just `filter(complexMethod(_, 2))`? What exactly did you want to achieve? – Andrey Tyukin Mar 22 '19 at 18:31
  • Your code looks almost exactly like [the code in this answer](https://stackoverflow.com/a/1550875/2707792). – Andrey Tyukin Mar 22 '19 at 18:33
  • @LuisMiguelMejíaSuárez oohhh!!! so if i change the underscore it just works. I didnt understand the reason why it doesnt work with the underscore – N.Car Mar 22 '19 at 18:35
  • 1
    @N.Car underscore has an _(many)_ especial meaning _(s)_ in **Scala**. Usually it is used to say "I do not care what is this", or "I do not want to give a name to this". You should never _(cant)_ use it as a normal variable name. – Luis Miguel Mejía Suárez Mar 22 '19 at 18:37
  • @AndreyTyukin I tried that and it works. What does that function do that the other doesnt. i guess its about the underscore. what does Int => Boolean really means? A function that takes only an Int and returns a Boolean? – N.Car Mar 22 '19 at 18:37
  • 1
    @N.Car Yes `A => B` means a function from **A** to **B**. – Luis Miguel Mejía Suárez Mar 22 '19 at 18:38
  • @LuisMiguelMejíaSuárez and it doesnt matter how many more arguments I provide as long as A is provided? – N.Car Mar 22 '19 at 18:40
  • 1
    My function has a single underscore in a position where a boolean is expected. That's what you probably wanted, and that's what the compiler understands. Your function discards its single argument, and returns a function from booleans to ints. That's something the compiler does not expect, and does not understand (it's unclear what this discarded argument is, and of what type it should be), and it's probably not what you wanted anyway. – Andrey Tyukin Mar 22 '19 at 18:41
  • 1
    @N.Car No. It _"clearly"_ says it is a function from **ONE** A to **ONE** B. Scala has a static and strong typesystem, you can only supply to functions / methods what they ask for, no more, no less. – Luis Miguel Mejía Suárez Mar 22 '19 at 18:42
  • @LuisMiguelMejíaSuárez how is it different from complexMethod(_, 2) , which also works. How does it still mean (A) ⇒ Boolean ? – N.Car Mar 22 '19 at 18:52
  • @N.Car from the answer of the duplicate: **What are all the uses of an underscore in Scala?** - _"Placeholder syntax"_. Basically, in that case `complexMethod(_, 2)` gets expanded to `i => complexMethod(i, 2)` _(you can change `i` with any name)_ where `i` is _inferred_ to be of type `Int` and since `complexMethod` returns a `Boolean` the type of the expression is `Int => Boolean`. – Luis Miguel Mejía Suárez Mar 22 '19 at 18:59
  • 1
    @LuisMiguelMejíaSuárez Yes! I just read it! its a placeholder for parameters in an anonymous function! Thank you so much for your help! – N.Car Mar 22 '19 at 19:01

0 Answers0