Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apparent Error in Higher Order Functions section #58

Open
erikwright opened this issue May 2, 2019 · 1 comment
Open

Apparent Error in Higher Order Functions section #58

erikwright opened this issue May 2, 2019 · 1 comment

Comments

@erikwright
Copy link

https://github.com/scala-exercises/exercises-scalatutorial/blob/master/src/main/scala/scalatutorial/sections/HigherOrderFunctions.scala#L126-L138

If I try to run code like this, I get:

Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_162-ea).
Type in expressions for evaluation. Or try :help.

scala> { def f(x1: Int, x2: Int) = x1 + x2 ; f }
<console>:12: error: missing argument list for method f
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `f _` or `f(_,_)` instead of `f`.
       { def f(x1: Int, x2: Int) = x1 + x2 ; f }
                                             ^

scala> 
@balath
Copy link

balath commented Jun 10, 2020

Hi @erikwright ;) !
It seems to me that error means that trying to run this code

def f(x1: Int, x2: Int) = x1 + x2 ; f

You're missing the arguments if you want to apply f, which you could fix like this

val nine = f(4, 5)

Or maybe you want to pass f as a parameter to a higher order function, in which case could use
f _
or
f(_, _)
in this way

def sum(f: (Int, Int) => Int, a: Int, b: Int): Int = f(a, b)
sum(f _, 4, 5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants