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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:17:15+00:00 2026-05-27T02:17:15+00:00

So I’ve been trying to puzzle through the various ways you can define stuff

  • 0

So I’ve been trying to puzzle through the various ways you can define stuff in Scala, complicated by my lack of understanding of the way {} blocks are treated:

object NewMain extends Thing{

    def f1 = 10
    def f2 {10}
    def f3 = {10}
    def f4() = 10
    def f5() {10}
    def f6() = {10}
    def f7 = () => 10
    def f8 = () => {10}
    def f9 = {() => {10}}

    def main(args: Array[String]){
        println(f1)     // 10
        println(f2)     // ()
        println(f3)     // 10
        println(f4)     // 10
        println(f4())   // 10
        println(f5)     // ()
        println(f5())   // ()
        println(f6)     // 10
        println(f6())   // 10
        println(f7)     // <function0>
        println(f7())   // 10
        println(f8)     // <function0>
        println(f8())   // 10
        println(f9)     // <function0>
        println(f9())   // 10
    }

}

Presumably some of these are equivalent, some of these are syntactic sugar for others, and some are things I should not use, but I can’t for the life of me figure it out. My specific questions are:

  • How is it that println(f2) and println(f5()) gives unit? Isn’t the last item in the block 10? How is it different from println(f3()), which gives 10?

  • If println(f5) gives unit, shouldn’t println(f5()) be invalid, since unit is not a function? The same applies to println(f6) and println(f6())

  • Of all the ones which print 10:
    f1, f3, f4, f4(), f6, f6(), f7(), f8(), f9(),
    is there any functional difference between them (in terms of what it does) or usage differences (in terms of when I should use which)? Or are they all equivalent?

  • 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-27T02:17:15+00:00Added an answer on May 27, 2026 at 2:17 am

    To answer your questions in order:

    • f2 and f5() return Unit because scala takes any def without an “=” to be a function that returns Unit, regardless of what the last item in a block is. This is a good thing, since otherwise it would not be rather verbose to define a function that does not return anything.
    • println(f5()) is valid, even though it returns Unit because in scala Unit is a valid object, though admittedly not one you can instantiate. Unit.toString() is a valid, if not generally useful, statement, for example.
    • Not all the versions that print out 10 are the same. Most importantly, f7,f8, and f9 are actually functions that return functions that return 10, rather than returning 10 directly. When you declare def f8 = () => {10}, you are declaring a function f8 that takes no arguments and returns a function that takes no arguments and returns a single integer. When you invoke println(f8) then f8 dilligently returns that function to you. When you call println(f8()) it returns the function, then immediately invokes it.
    • The functions f1,f3,f4, and f6 are all essentially equivalent in terms of what they do, they vary only in terms of style.

    As “user unknown” indicates, the braces are only important for scoping purposes and do not make any difference in your use case here.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.