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

Methods as functions (or, what exactly is “eta expansion”?)

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

Contents


More Related Answers


Methods as functions (or, what exactly is “eta expansion”?)

0

First we will see what’s the difference between no-parenthesis and empty-parenthesis methods. We will see what role this difference plays when converting methods to functions. Eta-expansion as a mechanism for transforming methods into functions will be explained, as well as techniques for manual conversion. Finally, partially applied functions and curried functions are explained in the context of the problem at hand.

If it’s empty, it still exists

Let’s say we have these two creatively named methods:

def methodA(s: String) = ???

def methodB(f: () => String) = ???

We want to see which invocations of these methods pass and which fail when we feed them with different values for parameters.

Let’s start from this very simple method f:

def f = "foo"

methodA(f)

methodB(f) // error!

methodA(f()) // error!

methodB(f()) // error!

Since methodA() takes a String, it will have no problems taking f as a parameter.  

Popularity 1/10 Helpfulness 1/10 Language whatever
Source: Grepper
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.