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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:44:38+00:00 2026-06-05T01:44:38+00:00

I think I still have not fully understood what type () represent during function

  • 0

I think I still have not fully understood what type () represent during function definition. So I can’t come up concrete example of each case: () => Int; Unit=> Int.

Can someone give me an easy example of function literal with type () => Int and type Unit=>Int respectively? After that, I think I can learn what () is exactly is.

Thanks

*EDIT: * destin says they are same. But the following test seems to indicate otherwise: () can’t be used in place expecting Unit.

scala> def inCase[A] ( b :Boolean, ifTrue : Unit => A, ifFalse : Unit => A ) : A  = 
 |     b match {
 |         case True => ifTrue()
 |         case _ => ifFalse()
 |     }

 inCase: [A](b: Boolean, ifTrue: Unit => A, ifFalse: Unit => A)A


scala> inCase( True,  () => 1,  () => -1 )
 <console>:11: error: type mismatch;
 found   : () => Int
  required: Unit => ?
          inCase( True,  () => 1,  () => -1 )
  • 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-05T01:44:40+00:00Added an answer on June 5, 2026 at 1:44 am

    () in a function type is not a type at all, nor is it a value. () => is simply syntax to indicate a function that takes zero arguments. See the BNF notation in section 3.2.9 of the Scala specification.

    When you see (Type1, Type2, Type3) => Type4, the () surrounds all of the argument types that the function takes. (Type1, Type2, Type3) is not a tuple type here — the parentheses are just syntax. So when you see () => Type4, you have an argument list that takes zero parameters. Single argument functions are special, in that you can omit the parentheses.

    Functions with no arguments, specified without the (), simply => Type4 are by-name parameters (which look like values, but are implemented as functions that get called implicitly when you try to evaluate their value.) They’re defined in section 4.6.1 of the spec.

    Parentheses can mean a bunch of other things in other contexts.

    • Parentheses in a function type declaration anywhere other than right before the => indicate a tuple type, and these can’t be empty. (Section 3.2.5)
    • Empty parentheses () are the return value of functions whose return type is Unit. (Section 12.2.3) This actually becomes a real value in certain contexts, for example println(println("foo")) will print

      foo                 <--- the argument to the inner println
      ()                  <--- the argument to the outer println
      
    • Parentheses are also syntax for function arguments when you make a function call, something that you’re probably already aware of. These can be empty. They’re defined in section 6.6 of the spec.

    • Parentheses are also syntax for function arguments when you define a method using def. These are described in section 4.6 of the spec.
    • Parentheses are also the syntax for creating tuple values (Section 6.9). If they’re not empty, you get a TupleN where N is the number of values inside of the parentheses. If they are empty, the type of this expression is Unit. (Sections 6.9, 12.2.3)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried nearly everything I can think of, but I'm still running into issues
I think I have a fairly basic question here. I'm not trying to waste
I have a bug in my code that made me think I don't fully
I'm not sure the title is the best possible, but I can't think of
Though it's on the edge of programming questions, I think this is still relevant
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Edit: I think I figured out the solution, but I'm still interested to know
I updated my python interpreter, but I think the old one is still called.
Think I have an activity with some layouts, I want to know is it
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).

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.