Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Partially applied functions

Sumit Rawal answered on May 16, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


Partially applied functions

0

There are times when compiler is not expecting a function so it will not automatically convert the method into a function. For example, let’s revisit our method f2() and try to define a corresponding function with same functionality:

def f2() = "foo"

val f2fun = f2 // f2fun is a string, not a function!

What happened? If you remember that stupid confusing automatic conversion from earlier, you will recall that it is allowed to invoke a parameterless method (that is, an empty-parenthesised method) without the parenthesis. OK, we agreed we would avoid doing this deliberately in our code in order to avoid confusion, but what do we do about the existing convention in the compiler that interprets our f2 as f2()? How do we turn the method f2() into a function value f2?

Well, there are two things we can do:

explicitly declare the type of value to be a function

treat the method as a partially applied function

def f2() = "foo"

val f2fun1 = f2 // f2fun is a string, not a function

val f2fun2: () => String = f2 // however, this works!

val f2fun3 = f2 _ // this too!  

Popularity 1/10 Helpfulness 1/10 Language whatever
Source: Grepper
Tags: whatever
Link to this answer
Share Copy Link
Contributed on May 16 2023
Sumit Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.