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 8633339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:28:38+00:00 2026-06-12T09:28:38+00:00

Suppose I want to assign an anonymous function which maps an Int to an

  • 0

Suppose I want to assign an anonymous function which maps an Int to an Int

I could assign it like this

val double = (x: Int) => x * 2

or like this

val double: Int => Int = x => x * 2

Which one is more idiomatic? Or are there situations where one is preferable to the other?

  • 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-12T09:28:38+00:00Added an answer on June 12, 2026 at 9:28 am

    I’d call it twice because double is a reserved word in Java and may prompt confusion with the data type Double (i.e. double in Java). In any case, neither is really more idiomatic than the other. All else being equal, I would prefer the first form since it is shorter and will automatically fix my types for me if I decide x should be a Float (i.e. I’ll only have to change the type in one spot). I’d favor the second if I decided it was important yet non-obvious that it was an Int => Int, or if the case was more complicated and it needed that information to do proper type inference. Actually, in this simple situation I’d write the cases as

    val twice = (x: Int) => x * 2
    val twice: Int => Int = _ * 2
    

    which makes them the same length. (I’d still favor the first one since it is no longer and in cases where you need to refer to the variable more than once you can keep the same form.)

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

Sidebar

Related Questions

Suppose this is a value in a JSON I want to assign to a
Suppose I want to make a Web application which uses a fixed width and
Suppose I want to implement an interpreter for a functional language. I would like
Suppose we want two constructors for a class representing complex numbers: Complex (double re,
This should be a fundamental C# question. Suppose I have a Person class which
let's suppose we have a javascript function and we want to check if the
Let's suppose we want to throw if we try to assign null to something,
Let's say I have an expression like this: Expression<Predicate<T>> exp If I assign the
Suppose I have an int-array and I want to modify it. I know that
Suppose I have an array of objects like this: var nodes = [{size:8,name:myName},{size:10,name:otherName},{size:11,name:userName}]; How

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.