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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:19:44+00:00 2026-05-23T01:19:44+00:00

Does anyone know the type of => Unit in scala? I don’t know the

  • 0

Does anyone know the type of => Unit in scala? I don’t know the meaning of => Unit and how to use it. I defined a function like below:

def test(code: => Unit){
   print("start ...")
   code
   print("end ....")
}

test(print(1))

Does it means a function with any arguments returning Unit?

Thanks

  • 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-23T01:19:45+00:00Added an answer on May 23, 2026 at 1:19 am

    This kind of parameter are called by-name parameter

    => B represents a block a of code which return a B value, their purpose is that they are evaluated only when you call the parameter.

    def foo(code: => Int) {
        println("Not yet evaluated")
        val result = code
        println("parameter evaluated %s, is it an int ? %s " format (
               result, result.isInstanceOf[Int]) )
    }
    

    And you can call foo in the following way :

    foo(1) 
    

    or

    val a = 3
    val b = 5
    foo {
      val c = a * a
      c * b
    }
    

    The other style of passing parameter is by-value : parameters are evaluated before they are sent to the method

    def foo(code : Int) {
        println("Parameter already evaluated")
        val result = code
        println("parameter evaluated : " + result)
    }
    

    References

    Extract from the Book Functionnal Programming in Scala

    More differences between by-name parameter and by-value parameter illustrated

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

Sidebar

Related Questions

Does anyone know why using-declarations don't seem to work for importing type names from
Does anyone know how to use packetoffset with a bool type in DirectX10/11? I
Does anyone know if there is some type of tool, preferably a Firefox add
Does anyone know if the data type DateTimeOffset is supported in the .NET Compact
Does anyone know how can I detect the browser type in css? What I
Does anyone know how to determine the active network type at the specific moment:
They seem to open in an internet explorer type window, does anyone know: How
Does anyone know how I can take a MySQL datetime data type value, such
The question is does anyone know what the structure of the (type-2) plist files
Does anyone know how to identify if an object passed to a function (classic

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.