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

  • Home
  • SEARCH
  • 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 3756708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:53:37+00:00 2026-05-19T09:53:37+00:00

I’m new to scala and trying to write a function literal that check whether

  • 0

I’m new to scala and trying to write a function literal that check whether a given integer is odd or not.
my first attempt is:

val isOdd = (x:Int) => (x & 1) == 1

it works great, and, since the parameter x only appears once within this function literal, I’m tempted to use the “_” notation to simplify it further, like this:

val isOdd = ((_:Int) & 1 ) == 1

however this time the compiler complains :

warning: comparing a fresh object using `==' will always yield false
val isOdd = ((_:Int) & 1 ) == 1

what does this warning mean? why does the compiler recognize ((_ :Int) & 1) as fresh object rather than a bitwise operation that results in a value? is there any way to write this function literal using the “_” notation?

  • 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-05-19T09:53:38+00:00Added an answer on May 19, 2026 at 9:53 am

    The problem is basically that Scala needs to tell the difference between

    val isOdd = ((_:Int) & 1 ) == 1
    

    where you want everything to the right of the equals sign to be a lambda, and

    val result = collection.map( _ + 1 )
    

    where you want only the stuff inside the parentheses to be a lambda

    Scala has decided that when you use the underscore to create a lambda, that it’s going to pick the innermost set of parentheses as the boundaries of that lambda. There’s one exception: (_:Int) doesn’t count as the innermost parentheses because its purpose is only to group they type declaration with the _ placeholder.

    Hence:

    val isOdd = ((_:Int) & 1 ) == 1
                ^^^^^^^^^^^^^^
                this is the lambda
    
    val result = collection.map( _ + 1 )
                                ^^^^^^^
                                this is the lambda
    
    val result = collection.map(( _ + 1) / 2)
                                ^^^^^^^^
                                this is the lambda
                                and the compiler can't infer the type of the _
    
    val result = somemap.map(( _ + 1) / 2 * _)
                             ^^^^^^^^
                             this is an inner lambda with one parameter
                             and the compiler can't infer the type of the _
                             ^^^^^^^^^^^^^^^^^
                             this is an outer lambda with one parameter
    

    This last case lets you do things like

    _.map(_ + 1)
    

    and have that get translated into

    x => x.map( y=> y + 1 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.