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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:52:22+00:00 2026-05-25T22:52:22+00:00

Possible Duplicate: Functions vs methods in Scala What is the difference between def foo

  • 0

Possible Duplicate:
Functions vs methods in Scala
What is the difference between def foo = {} and def foo() = {} in Scala?

In scala we can define

def foo():Unit = println ("hello")

or

def foo:Unit = println ("hello")

I know they are not the same but what is the difference, and which should be used when?

If this has been answered before please point me to that link.

  • 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-25T22:52:23+00:00Added an answer on May 25, 2026 at 10:52 pm

    A Scala 2.x method of 0-arity can be defined with or without parentheses (). This is used to signal the user that the method has some kind of side-effect (like printing out to std out or destroying data), as opposed to the one without, which can later be implemented as val.

    See Programming in Scala:

    Such parameterless methods are quite common in Scala. By contrast, methods defined with empty parentheses, such as def height(): Int, are called empty-paren methods. The recommended convention is to use a parameterless method whenever there are no parameters and the method accesses mutable state only by reading fields of the containing object (in particular, it does not change mutable state).

    This convention supports the uniform access principle […]

    To summarize, it is encouraged style in Scala to define methods that take no parameters and have no side effects as parameterless methods, i.e., leaving off the empty parentheses. On the other hand, you should never define a method that has side-effects without parentheses, because then invocations of that method would look like a field selection.

    Terminology

    There are some confusing terminology around 0-arity methods, so I’ll create a table here:

    Programming in Scala scala/scala jargon
    def foo: Int parameterless methods nullary method
    def foo(): Int empty-paren methods nilary method

    It might sound cool to say "nullary method", but often people say it wrong and the readers will also be confused, so I suggest sticking with parameterless vs empty-paren methods, unless you’re on a pull request where people are already using the jargons.

    () is no longer optional in Scala 2.13 or 3.0

    In The great () insert, Martin Odersky made change to Scala 3 to require () to call a method defined with (). This is documented in Scala 3 Migration Guide as:

    Auto-application is the syntax of calling a nullary method without passing an empty argument list.

    Note: Migration document gets the term wrong. It should read as:

    Auto-application is the syntax of calling a empty-paren (or "nilary") method without passing an empty argument list.

    Scala 2.13, followed Scala 3.x and deprecated the auto application of empty-paren methods in Eta-expand 0-arity method if expected type is Function0. A notable exception to this rule is Java-defined methods. We can continue to call Java methods such as toString without ().

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between a definition and a declaration? I am
Possible Duplicate: Special (magic) methods in Python who can tell me what can call
Possible Duplicate: Static extension methods So I know that Extension methods are for object
Possible Duplicate: Difference between pointer variable and reference variable in C++ suppose I'm trying
Possible Duplicate: Ruby functions vs methods Im just reading some ruby documentation and t
Possible Duplicate: Why does PHP 5.2+ disallow abstract static class methods? Why can't you
Possible Duplicate: What is the difference between a method and a function I'm trying
Possible Duplicate: [F#] How to have two methods calling each other? Hello all, I
Possible Duplicate: Can someone explain C++ Virtual Methods? I have a question regarding to
Possible Duplicate: How do you trigger javascript functions from flash? Possible Duplicate: How do

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.