Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:21:03+00:00 2026-06-11T22:21:03+00:00

e.g. val f1 = (a:Int) => a+1 def f2 = (a:Int) => a+1 Seems

  • 0

e.g.

val f1 = (a:Int) => a+1
def f2 = (a:Int) => a+1

Seems they are quite inter-changeable, are there any specific use case which I should use a particular one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:21:05+00:00Added an answer on June 11, 2026 at 10:21 pm

    Typically one would write the def version as:

    def f2(a:Int) = a+1
    

    If you write it they way you did, it’s actually creating a method f2 that, when called, constructs a new function closure. So in both of your cases, you still have a function. But in the first case, the function gets constructed only once and stored (as f1) for later use. But in the second case, the function gets constructed every time f2 is referenced, which is kind of a waste.

    So when to prefer a function over a method? Well, you can work with a function a bit more easily in certain situations. Consider this case:

    (1 to 3).map(f1 andThen f1)    // totally fine
    (1 to 3).map(f2 andThen f1)    // "missing arguments" error
    (1 to 3).map(f2 _ andThen f1)  // fixed by adding "_" after the method name
    

    So f1 is always a function and so you can call a method like andThen on it. The variable f2 represents a method, so it doesn’t expect to be used like this; it always expects to be followed by arguments. You can always turn a method into a function by following it with an underscore, but this can look a little ugly if it’s not necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unordered map: class O(val a: Int) Map[String, List[O]] which I'd like
Having val a: IndexedSeq[String] = Array(one, two, three) def f(s: String): Int = s
Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
Given: case class Foo(a: Int, b: String, c: Double) you can say: val params
If there is any way to call a def from a block def factor
int val = 233; byte b = (byte) val; System.out.println(b); I have a simple
public class Ex7 { private int fld; private void meth(int val) { fld =
// File: InitFirst.h #pragma once template <int val> struct InitFirst { static float s_dividedByThree;
A question about inheritance in java... class Base { private int val = 10;
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.